繁体   English   中英

Java在其中使用HashMap迭代ArrayList

[英]Java iterate over ArrayList with HashMap in it

我有一个有四个答案的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);  

现在我需要访问所有这些信息,所以我要做的是将两个HashMaps添加到一个ArrayList

    // Add the Hashmaps to the arrayList
    alleAntwoorden.add(antwoorden1);
    alleAntwoorden.add(antwoorden2);

但是我如何循环遍历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();
            // ...
        }  
    }

但我总是得到以下消息:不兼容的类型

Antwoorden1,antwoorden2和alleAntwoorden定义为:

private ArrayList<HashMap> alleAntwoorden; 
private HashMap<String, Boolean> antwoorden1, antwoorden2;

来自评论:

private ArrayList<HashMap> alleAntwoorden;

这就是问题。 您正在使用原始类型映射,但是您尝试将单个条目分配给变量Map.Entry<String, Boolean> 这不起作用,因为您当前的地图是HashMap<Object, Object> 将变量alleAntwoorden更改为:

private List<Map<String, Boolean>> alleAntwoorden;

请注意,我还将类型更改为其接口类型: 您是否始终使用Java编写接口

在以下界面上:

Map<String, Boolean> map1 = new HashMap<>();
Map<String, Boolean> map2 = new HashMap<>();
List<Map<String, Boolean>> list = new ArrayList<>();

我们可以使用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);
    }
}

一切都应该与以下定义一起使用:

Map<String, Boolean> antwoorden1 = new HashMap<>();
Map<String, Boolean> antwoorden2 = new HashMap<>();
List <Map<String, Boolean>> alleAntwoorden = new ArrayList<>();

如果您使用的是Eclipse,则可以编写entry.getValue() 将光标放在其上并使用键盘快捷键Ctrl + 2,l,它会自动解析正确的类型。

对于IntelliJ,它的工作方式几乎相同,但使用Ctrl + Alt + v

正如其他用户所说,有更好的方法来执行此类任务,但如果您想使用您的方法,这是一个正常运行的代码段:

    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
        }

    }

暂无
暂无

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

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