Bham
Bham

Reputation: 243

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

    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:

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

Upvotes: 16

Views: 50128

Answers (5)

ThisClark
ThisClark

Reputation: 14823

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:

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

Upvotes: 16

David Soroko
David Soroko

Reputation: 9106

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

Upvotes: 0

razcor
razcor

Reputation: 354

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
        }

    }

Upvotes: -1

Tom
Tom

Reputation: 17597

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:

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.

Upvotes: 21

Marcin Szymczak
Marcin Szymczak

Reputation: 11453

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.

For IntelliJ it works almost the same, but with Ctrl+Alt+v

Upvotes: 0

Related Questions