Java 在Hashmap中搜索值,添加该值+;键设置并返回已完成的设置
我有一个叫做Garage的类和第二个叫做customer的类,到目前为止Garage类的结构如下:Java 在Hashmap中搜索值,添加该值+;键设置并返回已完成的设置,java,hashmap,hashset,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 Gara
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 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");}
我现在希望能够搜索作为值保存的客户详细信息,例如:
字符串,字符串,int
将它们添加到新集合并返回。本质上,搜索原始Hashmap以查找唯一值,将key+值添加到集合中,并在搜索完完整Hashmap并添加所有值后返回完成的集合
希望没有把它弄糊涂!刚接触java,非常了解java,所以我非常感谢能得到的任何帮助:)你能试着用一些例子更好地解释一下吗?当然可以,Jacob,所以我有一个HashMap,其中包含say:,读作123ABC,name,Address,我想做的是在HashMap中搜索obj,并返回所有包含区域3或区域2的篮子/行?不幸的是,这仍然没有意义。我知道您目前有一张
地图
。您是说您想保留该地图中“面积”为2或3的每个客户吗?搜索完完整的Hashmap并添加所有值后,如果您可以编辑您的帖子并添加Customer.java
.Edited&Updated:)的代码,这将有所帮助。给定键只能有一个值。现在还不清楚你想用它做什么。你能试着用一些例子更好地解释一下吗?当然可以,Jacob,所以我有一个HashMap,其中包含say:读作123ABC,name,Address,我想做的是在HashMap中搜索obj,并返回所有包含区域3或区域2的篮子/行?不幸的是,这仍然没有意义。我知道您目前有一张地图
。您是说您想保留该地图中“面积”为2或3的每个客户吗?搜索完完整的Hashmap并添加所有值后,如果您可以编辑您的帖子并添加Customer.java
.Edited&Updated:)的代码,这将有所帮助。给定键只能有一个值。现在还不清楚你想用它做什么。
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");}