【问题标题】:Java - How do I loop through a map looking for instances of another classJava - 如何遍历地图以查找另一个类的实例
【发布时间】:2017-03-15 18:00:02
【问题描述】:

有问题的类是下面的类:

   public Customer(String aName, String anAddress, int anArea)
   {
      this.fullName = aName;
      this.address = anAddress;
      this.area = anArea;
   }

我像这样创建了地图(customers 变量单独初始化,创建了一个哈希图):

   public void addCustomer(String regNo, String name, String address, int area)
   {
      customers.put(regNo, new Customer(name, address, area));
   }

我希望程序做的是遍历地图,检查具有特定区域号码的客户。

方法头是这样的:

   public Customer findCustomersInArea(int area)
   {

   }

我创建了一个单独的方法,该方法使用 for each 循环遍历映射,该循环打印与 Customer 对象关联的所有详细信息,我尝试模拟此方法但无济于事。它看起来像这样:

    public void printCustomers()
   {
      Set<String> customerKeys = customers.keySet();
      for (String eachCustomer: customerKeys)
      {
         System.out.println (customers.get(eachCustomer));
      }
   }

我希望我已经清楚了,如果我需要提供更多详细信息,请告诉我。

【问题讨论】:

  • 嗯,到目前为止你做得很好。你现在在循环中只有getAreaCustomer 中的Customer,并将其与参数int area 进行比较。该方法看起来应该与您的printCustomers() 非常相似,但是在获取每个area 并进行比较时需要添加一些额外的代码,如果找到return that customer BUt 有更好的方法来执行 Map 的 getValueSet 所以你只需要担心Customer

标签: java loops methods maps


【解决方案1】:

此答案基于此SOURCE


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

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;
}

免责声明:代码是从我的手机即时编写的,因此可能包含一些拼写错误。

【讨论】:

    【解决方案2】:

    你的意思是这样的吗:

    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 文档:Map.values()

    【讨论】:

      【解决方案3】:

      如果 JDK >= 8 是负担得起的,您可以过滤 customers.values 以仅生成所需的值,例如:

      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());
      

      请注意,示例中的 list 分配将是您的 customers.values,然后只需替换 filter 调用中的条件。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-08-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-12-19
        相关资源
        最近更新 更多