[英]Looping through a List<Map<Integer, Map<Long, Integer>>> in Java
As my title suggests, I'm struggling with looping through the data structure given. 就像我的标题所暗示的那样,我正在努力遍历给定的数据结构。
I have a List of Maps which contain an Integer key and a Map value which contains a Long and an Integer. 我有一个包含Integer键的Map列表和一个包含Long和Integer的Map值。
How can I loop through the list, then map, then map to be able to access all of the required fields? 如何遍历列表,然后映射,然后映射才能访问所有必填字段?
Thanks. 谢谢。
EDIT: In response to the comments, the looping through the list I'm fine with. 编辑:为了回应评论,循环遍历我可以使用的列表。 So step one is to loop through the list, so I'm then left with: 因此,第一步是遍历列表,然后剩下的是:
for (Map<Integer, Map<Long, Integer>> periodScores : request.getPeriodScoreMap()) {
...
}
It's what goes in there that confuses me. 这就是让我感到困惑的东西。
Currently I have: 目前我有:
for (Map<Integer, Map<Long, Integer>> periodScores : request.getPeriodScoreMap()) {
while (periodScores.entrySet().iterator().hasNext()) {
Map<Integer, Map<Long, Integer>> opponentScores =
(Map<Integer, Map<Long, Integer>>) periodScores.entrySet().iterator().next();
}
}
And I'm struggling to get into that final Map of 我正在努力进入最终的地图
its just matter of being careful and being able to differentiate between map
and list
iterations 只是要小心并能够区分map
和list
迭代而已
here is just an example: 这只是一个例子:
for ( Map<Integer, Map<Long, Integer>> level1 : list){
for(Map.Entry<Integer,Map<Long, Integer>> level2 : level1.entrySet() ){
for(Map.Entry<Long,Integer> level3 : level2.getValue().entryList(){
}
}
}
Try this ;) 尝试这个 ;)
List<Map<Integer, Map<Long, Integer>>> myList = new LinkedList<>();
for(Map<Integer,Map<Long,Integer>> myListMap: myList){
for(Entry<Integer, Map<Long,Integer> myListMapEntry : myListMap.entrySet()){
Integer myListMapEntryInt = myListMapEntry.getKey();
for(Entry<Long, Integer> myListMapEntryValue : myListMapEntry.getValue()){
Long myListMapEntryValueLong = myListMapEntryValue.getKey();
Integer myListMapEntryValueInteger = myListMapEntryValue.getValue();
}
}
}
If the list of Maps is : List>> list; 如果地图列表是:List >> list; Then you can access each Integer in the innermost map as follows: 然后,您可以如下访问最里面的映射中的每个Integer:
for(Map<Integer,Map<Long,Integer>> outerMap : list){
for(Integer innerMapKeys : outerMap.keySet()){
Map<Long,Integer> innerMap = outerMap.get(innerMapKeys);
for(Long key:innerMap.keySet()){
System.out.println(innerMap.get(key));
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.