0

The class in question is the one below:

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

I created the map like so (the customers variable is initialised separately, creating a hashmap):

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

What I want the program to do is to iterate through the map, checking for customers that have a specific area number.

The method header looks like this:

   public Customer findCustomersInArea(int area)
   {

   }

I created a separate method that iterates through the map using a for each loop, which prints all the details associated with a Customer object and I have tried to emulate this method but to no avail. It looks like this:

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

I hope I have been clear, please let me know if I need to give more details.

aTubOfAdam
  • 91
  • 1
  • 7
  • Well you are doing good so far.. You have the `Customer` in the loop now just `getArea` of the `Customer` and compare it to the parameter `int area`. The method should look very similar to your `printCustomers()` but with a little extra code in their getting each `area` and comparing and if found `return that customer` BUt there is a better way of just doing getValueSet of the Map so you just have to worry about the `Customer` – 3kings Mar 15 '17 at 18:03
  • Possible duplicate of [How to efficiently iterate over each Entry in a Map?](http://stackoverflow.com/questions/46898/how-to-efficiently-iterate-over-each-entry-in-a-map) – StackFlowed Mar 15 '17 at 18:10

3 Answers3

2

This answer is based in this SOURCE.


Just iterate and look for what you need. In this example I create a List to return all customers that has this matching area:

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

Disclaimer: code is written on the fly from my phone, so it might contain some typo.

Community
  • 1
  • 1
Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
0

Do you mean something like this :

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

From the Java documentation : Map.values()

Bhacaz
  • 426
  • 5
  • 7
0

In case JDK >= 8 is affordable, you can filter customers.values to produce only desired values, like:

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

Notice that the list assignment in the example would be your customers.values then just replace the condition in filter call.

Shairon Toledo
  • 2,024
  • 16
  • 18