简体   繁体   English

遍历列表列表

[英]Iterating through Map of Maps of Lists

I have map like this 我有这样的map

Map<String, Map<String, List<String>>> m;

and i want to iterate through list 我想遍历列表

my logic is like this: 我的逻辑是这样的:

  1. first i getting the key of first map 首先,我得到第一张地图的钥匙
  2. and getting the key of secondmap 并获取secondmap的密钥
  3. and from that key i am iterating the list. 从那把钥匙我迭代列表。


for (Map m:m.keyset()){

    for (Map m1:m.getKey(){

        List<String> l = m1.getKey(){

        for (String s:l){
        }
    }
}

Is this correct? 这个对吗?

I am confused to test it...please help me...:) 我很困惑要测试...请帮助我... :)

You almost have it, here's what you're looking for 您几乎已经拥有了,这就是您想要的

Map<String,Map<String,List<String>>> m = new HashMap<String, Map<String,List<String>>>();

for(String k : m.keySet()) {
    Map<String,List<String>> m1 = m.get(k);

    for(String k1 : m1.keySet()) {
        List<String> l = m1.get(k1);

        for (String s : l){
        }
    }
}
Map<String, Map<String, List<String>>> m = new HashMap<>();

Iterate map using Entry : 使用Entry迭代地图:

for (Map.Entry<String, Map<String,List<String>> entry : m.entrySet()) {
    for (Map.Entry<String, List<String>> innerEntry : entry.entrySet()) {
        for (String elem : innerEntry) {
            ...
        }
    }
}

In terms of Java 8: 就Java 8而言:

m.forEach((s, entry) -> entry.forEach(
        (s1, innerEntry) -> innerEntry.forEach(
                elem -> { ... }
        )
));

Simply iterate through values 只需遍历值

    Map<String,Map<String,List<String>>> m = new HashMap<String, Map<String,List<String>>>();

    for(Map<String,List<String>> m2 : m.values()) {
        for(List<String> l : m2.values()) {
            for (String s : l){
            }
        }
    }

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

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