Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java—如何在映射中循环查找另一个类的实例_Java_Loops_Methods_Maps - Fatal编程技术网

Java—如何在映射中循环查找另一个类的实例

Java—如何在映射中循环查找另一个类的实例,java,loops,methods,maps,Java,Loops,Methods,Maps,所讨论的课程如下: public Customer(String aName, String anAddress, int anArea) { this.fullName = aName; this.address = anAddress; this.area = anArea; } 我这样创建了映射(customers变量单独初始化,创建一个hashmap): 我想让程序做的是遍历地图,检查有特定区域号的客户 方法标头如下所示: p

所讨论的课程如下:

   public Customer(String aName, String anAddress, int anArea)
   {
      this.fullName = aName;
      this.address = anAddress;
      this.area = anArea;
   }
我这样创建了映射(customers变量单独初始化,创建一个hashmap):

我想让程序做的是遍历地图,检查有特定区域号的客户

方法标头如下所示:

   public Customer findCustomersInArea(int area)
   {

   }
    public void printCustomers()
   {
      Set<String> customerKeys = customers.keySet();
      for (String eachCustomer: customerKeys)
      {
         System.out.println (customers.get(eachCustomer));
      }
   }
我创建了一个单独的方法,该方法使用for-each循环遍历映射,该循环打印与客户对象关联的所有详细信息,我尝试模拟该方法,但没有效果。看起来是这样的:

   public Customer findCustomersInArea(int area)
   {

   }
    public void printCustomers()
   {
      Set<String> customerKeys = customers.keySet();
      for (String eachCustomer: customerKeys)
      {
         System.out.println (customers.get(eachCustomer));
      }
   }
public void printCustomers()
{
Set customerKeys=customers.keySet();
for(字符串每个客户:customerKeys)
{
System.out.println(customers.get(每个客户));
}
}

我希望我已经说清楚了,如果我需要提供更多的细节,请告诉我。

这个答案基于此


只需迭代并查找您需要的内容。 在本例中,我创建了一个
列表
,以返回具有此匹配区域的所有客户

public List<Customer> findCustomersInArea(int area)
{
    // create a list to store all Customer with given area
    List<Customer> inArea = new ArrayList<Customer>();
    // iterate all entries in customers map
    for (Map.Entry<String, Customer> entry : customers.entrySet())
    {
        // get the customer
        Customer c = entry.getValue();
        if (c.getArea() == area) {
            // do what you need, for example add to a list
            inArea.add(c);
        }
    }
    // return the list with all customers
    return inArea;
}
公共列表查找客户区域(内部区域)
{
//创建一个列表以存储给定区域的所有客户
List inArea=new ArrayList();
//迭代客户映射中的所有条目
for(Map.Entry:customers.entrySet())
{
//抓住顾客
客户c=entry.getValue();
if(c.getArea()==area){
//做你需要的事情,例如添加到列表中
添加(c);
}
}
//与所有客户一起返回列表
返回伊纳里亚;
}

免责声明:代码是从我的手机中动态编写的,因此可能包含一些打字错误。

此答案基于此


只需迭代并查找您需要的内容。 在本例中,我创建了一个
列表
,以返回具有此匹配区域的所有客户

public List<Customer> findCustomersInArea(int area)
{
    // create a list to store all Customer with given area
    List<Customer> inArea = new ArrayList<Customer>();
    // iterate all entries in customers map
    for (Map.Entry<String, Customer> entry : customers.entrySet())
    {
        // get the customer
        Customer c = entry.getValue();
        if (c.getArea() == area) {
            // do what you need, for example add to a list
            inArea.add(c);
        }
    }
    // return the list with all customers
    return inArea;
}
公共列表查找客户区域(内部区域)
{
//创建一个列表以存储给定区域的所有客户
List inArea=new ArrayList();
//迭代客户映射中的所有条目
for(Map.Entry:customers.entrySet())
{
//抓住顾客
客户c=entry.getValue();
if(c.getArea()==area){
//做你需要的事情,例如添加到列表中
添加(c);
}
}
//与所有客户一起返回列表
返回伊纳里亚;
}

免责声明:代码是从我的手机中动态编写的,因此可能包含一些打字错误。

您的意思是这样的:

public Customer findCustomersInArea(int area){
    for(Customer c : customers.values()){  // iterate throw each Customer in the Map 
        if(c.getArea() == area)  // if the area of the customer is the one you search
            return c;   // return the customer
    }
}

从Java文档中:

您的意思是这样的:

public Customer findCustomersInArea(int area){
    for(Customer c : customers.values()){  // iterate throw each Customer in the Map 
        if(c.getArea() == area)  // if the area of the customer is the one you search
            return c;   // return the customer
    }
}

从Java文档中:

如果JDK>=8价格合理,您可以过滤
客户。值
以仅生成所需的值,如:

class Customer {
  String name, address;
  int area;

  public Customer(String name, String address, int area){
    this.name = name;
    this.address = address;
    this.area = area;
  }
  @Override
  public String toString(){
    return String.join(", ",name, address, area+"");
  }
}

List<Customer> list = Stream.of(
  new Customer("foo1", "address1", 1 ),
  new Customer("foo2", "address2", 2 ),
  new Customer("foo3", "address3", 3 ),
  new Customer("foo4", "address4", 4 )
).collect(Collectors.toList());

List<Customer> selectedCustomers = list.stream()
  .filter(c -> c.area > 2)
  .collect(Collectors.toList());
class客户{
字符串名称、地址;
内部区域;
公共客户(字符串名称、字符串地址、整数区域){
this.name=名称;
this.address=地址;
这个面积=面积;
}
@凌驾
公共字符串toString(){
返回字符串。join(“,”,名称,地址,区域+”);
}
}
List List=Stream.of(
新客户(“foo1”、“address1”、1),
新客户(“foo2”、“地址2”、2),
新客户(“foo3”、“address3”、3),
新客户(“foo4”、“address4”、4)
).collect(Collectors.toList());
List selectedCustomers=List.stream()
.过滤器(c->c.area>2)
.collect(Collectors.toList());

请注意,示例中的
列表
分配将是您的
客户。值
然后只需替换
过滤器
调用中的条件。

如果JDK>=8价格合理,您可以过滤
客户。值
只生成所需的值,如:

class Customer {
  String name, address;
  int area;

  public Customer(String name, String address, int area){
    this.name = name;
    this.address = address;
    this.area = area;
  }
  @Override
  public String toString(){
    return String.join(", ",name, address, area+"");
  }
}

List<Customer> list = Stream.of(
  new Customer("foo1", "address1", 1 ),
  new Customer("foo2", "address2", 2 ),
  new Customer("foo3", "address3", 3 ),
  new Customer("foo4", "address4", 4 )
).collect(Collectors.toList());

List<Customer> selectedCustomers = list.stream()
  .filter(c -> c.area > 2)
  .collect(Collectors.toList());
class客户{
字符串名称、地址;
内部区域;
公共客户(字符串名称、字符串地址、整数区域){
this.name=名称;
this.address=地址;
这个面积=面积;
}
@凌驾
公共字符串toString(){
返回字符串。join(“,”,名称,地址,区域+”);
}
}
List List=Stream.of(
新客户(“foo1”、“address1”、1),
新客户(“foo2”、“地址2”、2),
新客户(“foo3”、“address3”、3),
新客户(“foo4”、“address4”、4)
).collect(Collectors.toList());
List selectedCustomers=List.stream()
.过滤器(c->c.area>2)
.collect(Collectors.toList());

请注意,示例中的
列表
分配将是您的
客户。值
然后只需替换
过滤器
调用中的条件。

到目前为止,您做得很好。。现在循环中有
Customer
,只需
Customer
getArea
,并将其与参数
int area
进行比较。该方法应该与
printCustomers()非常相似
但是在他们获取每个
区域并进行比较的过程中需要一些额外的代码,如果发现
则返回该客户
,但是有一种更好的方法来完成地图的getValueSet,因此您只需担心
客户
到目前为止您所做的工作可能重复。。现在循环中有
Customer
,只需
Customer
getArea
,并将其与参数
int area
进行比较。该方法看起来应该非常类似于您的
printCustomers()
,但是在获取每个
区域
并进行比较时会有一些额外的代码,如果找到
,则返回该客户
,但是有一种更好的方法只是执行映射的getValueSet,因此您只需担心
客户
可能会重复