0
I have this code here : 
 public static void exercise5e() {
    List<Hills> hillist = Hills.readHills();

public static Map> hillsByCounty(List hills) {

    HashMap<String, Set<Hills>> hashMap = new HashMap<String, Set<Hills>>();
    // List<Hills> hillist = Hills.readHills();

    for (Hills h : hillist) {
        String key = h.countryname;

        } else {
            Set<Hills> set = new TreeSet<Hills>();
            set.add(h);
            hashMap.put(key, set);

        }

    }





    System.out.println(hashMap1);
    return hashMap1;
}

} My hashmap has values of ( hill name , country name , height, longitude and latitude from a list however i need to print only 3 counties with the first 3 hills of each counties and their height how can i do that?enter image description here

Fizz
  • 37
  • 8
  • Possible duplicate of [Java Hashmap: How to get key from value?](http://stackoverflow.com/questions/1383797/java-hashmap-how-to-get-key-from-value) – Fizz Apr 05 '17 at 23:13

1 Answers1

0

That's how your method should look like. I think that's the best one can do without the actual code.

    List<Hills> hillist = Hills.readHills();
    Map<String, Set<Hills>> hillsByCounty = Hills.hillsByCounty(hillist);

    Set<Map.Entry<String, Set<Hills>>> entrySet = hashMap.entrySet();
    Iterator<Map.Entry<String, Set<Hills>>> entryIterator = entry1.iterator();
    for(int i = 0; i < 3; i++){
            Map.Entry<String, Set<Hills>> mapEntry = entryIterator.next();
            System.out.println("### County: " + mapEntry.getKey());
            Set<Hills> hills = mapEntry.getValue();
            for (int j = 0; j < 3; j++) {     
                    Hill hill = iterator.next();
                    System.out.println(hill.hillName + " " + hill.height);
                }
            }
        }
    }

Change hill.hillName and hill.height to whatever variable names for hill name and hill height are.

Fizz
  • 37
  • 8
Cargeh
  • 1,029
  • 9
  • 18
  • Should break out after 3 counties, since OP wants to print only 3 counties. – Crocode Apr 04 '17 at 23:07
  • Well to be honest yes it is working but is not breaking after 3 counties , it is going until the end of the whole list – Fizz Apr 05 '17 at 00:29
  • Simplest solution: add counter, and then after each iterator increment it and check if it = 3. I've edited my answer – Cargeh Apr 05 '17 at 19:20
  • I've changed it again so that it uses the iterator from the code you provided. I think that's what's expected of you. – Cargeh Apr 05 '17 at 19:40