Java 使用 HashMap 遍历 ArrayList
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27485794/
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
Java iterate over ArrayList with HashMap in it
提问by Bham
I have a Hashmap with four answers. And I have for ex 2 questions. This is how i do it
我有一个带有四个答案的 Hashmap。我有前 2 个问题。这就是我的做法
// Awnsers question 1
antwoorden1.put("Hypertext Preprocessor", true);
antwoorden1.put("Hypertext PHPprocessor", false);
antwoorden1.put("Hypertext processor", false);
antwoorden1.put("Preprocessor PHP", false);
// Awnsers question 2
antwoorden2.put("Model view config", false);
antwoorden2.put("Model view connect", false);
antwoorden2.put("Model view controllers", false);
antwoorden2.put("Model view controller", true);
Now I need to get access to all this information, so what I do is add the two HashMaps to one ArrayList
现在我需要访问所有这些信息,所以我要做的是将两个 HashMap 添加到一个 ArrayList
// Add the Hashmaps to the arrayList
alleAntwoorden.add(antwoorden1);
alleAntwoorden.add(antwoorden2);
But how can I loop through the ArrayList to get the key and value from the HashMap? This is what I already tried.
但是如何遍历 ArrayList 以从 HashMap 中获取键和值?这是我已经尝试过的。
for(int i = 0; i < alleAntwoorden.size(); i++)
{
for (Map.Entry<String, Boolean> entry : alleAntwoorden.get(i).entrySet())
{
String key = entry.getKey();
Object value = entry.getValue();
// ...
}
}
But I always get the following msg: incompatible types
但我总是收到以下消息:不兼容的类型
Antwoorden1, antwoorden2 and alleAntwoorden are defined as:
Antwoorden1、antwoorden2 和 alleAntwoorden 定义为:
private ArrayList<HashMap> alleAntwoorden;
private HashMap<String, Boolean> antwoorden1, antwoorden2;
采纳答案by ThisClark
On the following interfaces:
在以下接口上:
Map<String, Boolean> map1 = new HashMap<>();
Map<String, Boolean> map2 = new HashMap<>();
List<Map<String, Boolean>> list = new ArrayList<>();
We may iterate with foreach loops:
我们可以使用 foreach 循环进行迭代:
for (Map<String, Boolean> entry : list) {
for (String key : entry.keySet()) {
Boolean value = entry.get(key);
System.out.println("key = " + key);
System.out.println("value = " + value);
}
}
回答by Marcin Szymczak
If you are using Eclipse you can just write entry.getValue()
. Put cursor on top of it and use keyboard shortcut Ctrl+2, l, which automatically resolves correct type.
如果您使用的是 Eclipse,则只需编写entry.getValue()
. 将光标放在它上面并使用键盘快捷键 Ctrl+2, l,它会自动解析正确的类型。
For IntelliJ it works almost the same, but with Ctrl+Alt+v
对于 IntelliJ,它的工作原理几乎相同,但使用 Ctrl+Alt+v
回答by Tom
From the comment:
从评论:
private ArrayList<HashMap> alleAntwoorden;
This is the problem. You're using a raw type map, but you're trying to assign a single entry to the variable Map.Entry<String, Boolean>
. This cannot work, because your current map is of type HashMap<Object, Object>
. Change the variable alleAntwoorden
to:
这就是问题。您正在使用原始类型映射,但您试图将单个条目分配给变量Map.Entry<String, Boolean>
。这行不通,因为您当前的地图类型为HashMap<Object, Object>
。将变量更改alleAntwoorden
为:
private List<Map<String, Boolean>> alleAntwoorden;
Mind, that I've also changed the types to their Interface type: Should you always Code To Interfaces In Java.
请注意,我还将这些类型更改为它们的接口类型:您是否应该始终在 Java 中编写接口代码。
回答by razcor
As stated by other users, there are better ways to do this kind of task, but if you want to use your approach, this a functioning code snippet:
正如其他用户所说,有更好的方法来完成这种任务,但如果你想使用你的方法,这是一个功能性的代码片段:
HashMap antwoorden1 = new HashMap();
HashMap antwoorden2 = new HashMap();
// Awnsers question 1
antwoorden1.put("Hypertext Preprocessor", true);
antwoorden1.put("Hypertext PHPprocessor", false);
antwoorden1.put("Hypertext processor", false);
antwoorden1.put("Preprocessor PHP", false);
// Awnsers question 2
antwoorden2.put("Model view config", false);
antwoorden2.put("Model view connect", false);
antwoorden2.put("Model view controllers", false);
antwoorden2.put("Model view controller", true);
ArrayList<HashMap> alleAntwoorden = new ArrayList<HashMap>();
// Add the Hashmaps to the arrayList
alleAntwoorden.add(antwoorden1);
alleAntwoorden.add(antwoorden2);
for(int i = 0; i < alleAntwoorden.size(); i++)
{
Iterator it = (Iterator)alleAntwoorden.get(i).entrySet().iterator();
while (it.hasNext()) {
Map.Entry pairs = (Map.Entry)it.next();
System.out.println(pairs.getKey() + " = " + pairs.getValue());
it.remove(); // avoids a ConcurrentModificationException
}
}
回答by David Soroko
Everything should work with the following definitions:
一切都应符合以下定义:
Map<String, Boolean> antwoorden1 = new HashMap<>();
Map<String, Boolean> antwoorden2 = new HashMap<>();
List <Map<String, Boolean>> alleAntwoorden = new ArrayList<>();