在 Java 中迭代 List<Map<String, String>>
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31041740/
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
Iterate over List<Map<String, String>> in Java
提问by Syed
I'm trying to iterate List<Map<String, String>>
in Java. However, I'm not able to iterate it properly. Can any one guide me?
我正在尝试List<Map<String, String>>
在 Java 中进行迭代。但是,我无法正确迭代它。任何人都可以指导我吗?
Iterator it = list.iterator();
while (it.hasNext()) {
Map.Entry pairs = (Map.Entry) it.next();
System.out.println(pairs.getKey() + " = " + pairs.getValue());
}
Thanks,
谢谢,
回答by Ankur Singhal
public static void main(String[] args) {
List<Map<String, String>> myListOfMaps = new ArrayList<Map<String, String>>();
Map<String, String> map1 = new HashMap<String, String>();
map1.put("Fname", "Ankur");
Map<String, String> map2 = new HashMap<String, String>();
map2.put("Lname", "Singhal");
myListOfMaps.add(map1);
myListOfMaps.add(map2);
for (int i = 0 ; i < myListOfMaps.size() ; i++) {
Map<String, String> myMap = myListOfMaps.get(i);
System.out.println("Data For Map" + i);
for (Entry<String, String> entrySet : myMap.entrySet()) {
System.out.println("Key = " + entrySet.getKey() + " , Value = " + entrySet.getValue());
}
}
}
output
输出
Data For Map0
Key = Fname , Value = Ankur
Data For Map1
Key = Lname , Value = Singhal
回答by YoYo
Forget using the iterator directly, why not simply this:
忘记直接使用迭代器,为什么不简单地这样:
List<Map<String,String>> l = new ArrayList<>();
...
// add map elements to list
...
for (Map<String,String> m:l) {
for (Map.Entry<String,String> e:m.entrySet()) {
String key = e.getKey();
String value = e.getValue();
// Do something with key/value
}
}
This is called an Enhanced for Loop. Internally it will handle it as a for loop traversing the iterator of any collection, or any other implementation of the Iterable
Interface.
这称为增强的 for 循环。在内部,它将作为遍历任何集合的迭代器或Iterable
接口的任何其他实现的 for 循环来处理它。
It was already used for traversing the Map Entries in one answer, so why not for the list of maps?
它已经用于在一个答案中遍历地图条目,那么为什么不用于地图列表呢?
Of course, for nested collections, you also need to know how to nest your for-loops (how you put one for loop inside the other).
当然,对于嵌套集合,您还需要知道如何嵌套 for 循环(如何将一个 for 循环放入另一个循环中)。
回答by QBrute
You iterate over a list with elements of type Map<String, String>
. So casting to Map.Entry
will give you a ClassCastException
.
您迭代具有类型元素的列表Map<String, String>
。所以投射到Map.Entry
会给你一个ClassCastException
.
Try it like this
像这样试试
Iterator it = list.iterator();
while (it.hasNext()) {
Map<String, String> map = (Map<String, String>) it.next();
for (Map.Entry entry : map.entrySet()) {
System.out.println(entry.getKey() + " = " + entry.getValue());
}
}
It would be easier for you, if you didn't use the raw types Iterator
and Map.Entry
. Use generics wherever possible. So the code would look like this:
如果您不使用原始类型Iterator
和Map.Entry
. 尽可能使用泛型。所以代码看起来像这样:
Iterator<Map<String, String>> it = list.iterator();
while (it.hasNext()) {
Map<String, String> map = it.next(); //so here you don't need a potentially unsafe cast
for (Map.Entry<String, String> entry : map.entrySet()) {
System.out.println(entry.getKey() + " = " + entry.getValue());
}
}
回答by Burkhard
list has no entySet() method!
list 没有 entySet() 方法!
Try this:
试试这个:
final Iterator<Map<String, String>> it = list.iterator();
while (it.hasNext())
{
Map<String, String> mapElement = it.next();
// do what you want with the mapElement
}
Of course, you will need another loop to iterate over the elements in the map.
当然,您将需要另一个循环来迭代地图中的元素。