Java:遍历另一个 HashMap 中的 HashMap

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31242039/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 18:19:19  来源:igfitidea点击:

Java: Iterate through a HashMap which is inside another HashMap

javahashmap

提问by Chanuka Ranaba

I want to iterate through a HashMapwhich is inside another HashMap

我想遍历HashMap另一个内部的aHashMap

Map<String, Map<String, String>> PropertyHolder

I was able to iterate through the parent HashMapas following,

我能够HashMap按如下方式遍历父级,

Iterator it = PropertyHolder.entrySet().iterator();
while (it.hasNext()) {
  Map.Entry pair = (Map.Entry) it.next();
  System.out.println("pair.getKey() : " + pair.getKey() + " pair.getValue() : " + pair.getValue());
  it.remove(); // avoids a ConcurrentModificationException
}

but could not able to iterate through the child Map, It can be done by converting pair.getValue().toString()and separated using ,and =. Is there any other way of iterating it?

但无法遍历子代Map,可以通过pair.getValue().toString()使用,and转换和分隔来完成=。有没有其他迭代方法?

采纳答案by seanhodges

You could iterate the child map similar to how you've done the parent:

您可以迭代子映射类似于您完成父级的方式:

Iterator<Map.Entry<String, Map<String, String>>> parent = PropertyHolder.entrySet().iterator();
while (parent.hasNext()) {
    Map.Entry<String, Map<String, String>> parentPair = parent.next();
    System.out.println("parentPair.getKey() :   " + parentPair.getKey() + " parentPair.getValue()  :  " + parentPair.getValue());

    Iterator<Map.Entry<String, String>> child = (parentPair.getValue()).entrySet().iterator();
    while (child.hasNext()) {
        Map.Entry childPair = child.next();
        System.out.println("childPair.getKey() :   " + childPair.getKey() + " childPair.getValue()  :  " + childPair.getValue());

        child.remove(); // avoids a ConcurrentModificationException
    }

}

I've presumed you want to call .remove()on the child map, which will lead to a ConcurrentModificationException if done while looping the entrySet - it looks as though you discovered this already.

我假设您想调用.remove()子映射,如果在循环 entrySet 时完成,这将导致 ConcurrentModificationException - 看起来好像您已经发现了这一点。

I've also swapped out your use of casting with strongly-typed generics as suggested in the comments.

我还按照评论中的建议,将您对强类型泛型的使用换掉了。

回答by griFlo

    for (Entry<String, Map<String, String>> entry : propertyHolder.entrySet()) {
        Map<String, String> childMap = entry.getValue();

        for (Entry<String, String> entry2 : childMap.entrySet()) {
            String childKey = entry2.getKey();
            String childValue = entry2.getValue();
        }
    }

回答by duffymo

It's obvious - you need two nested loops:

很明显 - 您需要两个嵌套循环:

for (String key1 : outerMap.keySet()) {
    Map innerMap = outerMap.get(key1);
    for (String key2: innerMap.keySet()) {
        // process here.
    }
}