如何使用列表迭代嵌套的hashmap

时间:2016-03-14 07:59:59

标签: java list collections hashmap

嵌套HashMap:

HashMap<String,HashMap<String,String>> outerMap=new HashMap<String, HashMap<String, String>>();
    HashMap<String,String> innerhashMap=new HashMap<String, String>();
    innerhashMap.put("aaa","AAA");
    outerMap.put("111",innerhashMap);
    innerhashMap.put("aaa","AAA");
    outerMap.put("222",innerhashMap);

我想要外部地图键列表,内部地图键列表和内部地图值列表

2 个答案:

答案 0 :(得分:0)

for ( String outerkey : outerMap.keySet() ) {
   HashMap<String,String> innerHashMap = (HashMap<String,String>) outerMap.get(outerKey)
   for ( String innerKey : innerHashMap.keySet() ) {
        String innerValue = (String) innerHashMap.get(innerKey);
        //... Process them
   }
}

答案 1 :(得分:0)

HashMap<String,HashMap<String,String>> outerMap = new HashMap<>();
HashMap<String,String> innerhashMap = new HashMap<>();
innerhashMap.put("aaa","AAA");
outerMap.put("111",innerhashMap);
innerhashMap.put("aaa","AAA");
outerMap.put("222",innerhashMap);
outerMap.forEach((k, v) -> {
    System.out.println("OUTER KEY: " +k);
    v.forEach((kk, vv) -> {
        System.out.println("INNER KEY: " +kk+ " INNER VALUE: " +vv);
    });
});