简体   繁体   English

如何从Hashmap提取和打印特定值?

[英]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) { 公共静态地图> hillsByCounty(列出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 }我的哈希图从列表中具有(丘陵名称,国家名称,高度,经度和纬度的值,但是我只需要打印3个县,每个县的前3个丘陵及其高度,我该怎么做? 在此处输入图像描述

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. hill.hillNamehill.height更改为hill name和hill height的变量名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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