Bham Bham - 6 months ago 51
Java Question

Java iterate over ArrayList with HashMap in it

I have a Hashmap with four answers. And I have for ex 2 questions. This is how i do it

// 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

// Add the Hashmaps to the arrayList

But how can I loop through the ArrayList to get the key and value from the HashMap? This is what I already tried.

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 arde defined as:

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


With the following variables implemented on interfaces:

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

We may iterate them with foreach loops (assuming data and maps are added to the list):

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);