简体   繁体   中英

How i can extract and print specific values from a Hashmap?

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM