在Hashmap中搜索值,添加该值+键以设置并返回已完成的集合

时间:2017-04-25 21:32:45

标签: java hashmap hashset

所以我有一个名为Garage的类和一个名为customer的第二个类,到目前为止,Garage类构造如下:

public class Garage

private Map <String, Customer> customers;

/**
* Constructor for objects of class Garage, implements the customers instance 
variable and assigns it a HashMap
*/
public Garage()
{
  customers = new HashMap<String, Customer>(); 
}

/**
* Adds a Customer to the Garage Hashmap customers. 
*/
public void addCustomer (String regNo, String name, String address, int area)
{
  Customer aCustomer = new Customer (name,address, area);
  this.customers.put(regNo,aCustomer) ;

}

/**
* Prints out a list of customers from the HashMap customers. 
*/
public void printCustomers()
{
 Set set = customers.entrySet();
  Iterator cusIterator = set.iterator();

 while (cusIterator.hasNext()){
    Map.Entry mentry = (Map.Entry)cusIterator.next();
    System.out.println(mentry.getValue());}
  }

  /**
   * Finds Customer information from customers Hashmap based on registration 
 key. 
   */
  public void findCustomerWithReg(String aReg)
  {
     if (customers.containsKey(aReg)){
        System.out.println (aReg +" "+ customers.get(aReg));
  }
     else {
        System.out.println (aReg + " is not found in the database");}

}

然后客户看起来像:

public class Customer
{

private String fullName;
private String address;
private int area;

/**
* Constructor for objects of class Customer
*/
public Customer(String aName, String anAddress, int anArea)
{
  // initialise instance variables
  this.fullName = aName;
  this.address = anAddress;
  this.area = anArea;
}

public String getName()
{
  return this.fullName;
}

public String getAddress()
{
  return this.address;
}

public int getArea()
{
  return this.area;
}

public String toString()
{
  return (this.getName() + " " + this.getAddress() + " area: " + 
 this.getArea());
 }
 } 

我的问题在于Garage Class,特别是这种方法:

public void findCustomerWithReg(String aReg)
  {
     if (customers.containsKey(aReg)){
        System.out.println (aReg +" "+ customers.get(aReg));
  }
     else {
        System.out.println (aReg + " is not found in the database");}

我现在希望能够搜索作为价值的客户详细信息,例如:

String,String,int

将它们添加到新集并返回它们。本质上搜索原始Hashmap以查找唯一值,将key +值添加到集合中,并在搜索完整的Hashmap并添加所有值后返回已完成的集合。

希望它没有过分混淆它! Java的新手,非常学习,所以我很感激任何帮助,我可以得到:)

0 个答案:

没有答案