16

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;
ThisClark
  • 14,352
  • 10
  • 69
  • 100
Bham
  • 245
  • 1
  • 2
  • 10
  • 5
    `boolean value = entry.getValue();` and not `Object`. And `alleAntwoorden` should be of type `List>`. – Tom Dec 15 '14 at 14:04
  • 3
    How are `antwoorden1`, `antwoorden2` and `alleAntwoorden` defined? – Predrag Maric Dec 15 '14 at 14:04
  • Could you provide the detailed error message? – MD Sayem Ahmed Dec 15 '14 at 14:12
  • In this case, I'm not sure how the HashMap is practical. An ArrayList storing 4 Answers that contain a String and a Boolean would probably be more appropriate. – Compass Dec 15 '14 at 14:13
  • Antwoorden1, antwoorden2 and alleAntwoorden arde defined as: private ArrayList alleAntwoorden; private HashMap antwoorden1, antwoorden2; – Bham Dec 15 '14 at 14:14
  • How could you store in a ArrayList a String and Boolean? – Bham Dec 15 '14 at 14:15
  • `class Answer{String question;boolean value;}` – Compass Dec 15 '14 at 14:17
  • @user3704388 I guess Compass meant a type `Answer{string; boolean}` – Kent Dec 15 '14 at 14:17
  • 1
    @user3704388 can you tell a bit, why you want to iterate all answers? I feel that your design may have problem, the `List>` is not convenient. You can consider Compass's `Answer` Type approach. – Kent Dec 15 '14 at 14:19
  • a List> type is also much easier to iterate through. I love Maps as much as anyone else, but accessing Map items is much harder when you're using the Map as a 4x2 array rather than as a key/value pair. – Compass Dec 15 '14 at 14:23

5 Answers5

21

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.

Community
  • 1
  • 1
Tom
  • 16,842
  • 17
  • 45
  • 54
15

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);
    }
}
ThisClark
  • 14,352
  • 10
  • 69
  • 100
  • 1
    Hiya, this may well solve the problem... but it'd be good if you could provide a little explanation about how and why it works :) Don't forget - there are heaps of newbies on Stack Overflow, and they could learn a thing or two from your expertise - what's obvious to you might not be so to them. Also... did you hear of the Diamond Operator? Your generic declarations could be drastically shortened with it – Vogel612 Dec 15 '14 at 14:16
  • This is almost certainly the answer that the OP needs, but @Vogel612 is right - it would benefit from an explanation that you are specifying the types for the generic Hashmap in the Arraylist i.e. the difference between ArrayList> alleAntwoorden and ArrayList alleAntwoorden – J Richard Snape Dec 15 '14 at 14:21
  • I haven't heard of diamond operator, but I just did a search of it - gives me something to read this morning. Thanks. – ThisClark Dec 15 '14 at 14:22
  • 1
    @ThisClark if you are programming in Java 6, it is unlikely that you have seen it. It is part of Java 7 on. It keeps the code tidy for SO. – Compass Dec 15 '14 at 14:25
  • You can avoid the extra lookups by iterating over the `entrySet()`. Or in Java 8, you can use the even cleaner `forEach()`. – shmosel Dec 22 '17 at 03:30
0

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

Marcin Szymczak
  • 11,199
  • 5
  • 55
  • 63
0

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<>();
David Soroko
  • 8,521
  • 2
  • 39
  • 51
  • 2
    Hiya, this may well solve the problem... but it'd be good if you could provide a little explanation about how and why it works :) Don't forget - there are heaps of newbies on Stack Overflow, and they could learn a thing or two from your expertise - what's obvious to you might not be so to them. – Vogel612 Dec 15 '14 at 14:25
  • This can be a starting point for learning more - if one chooses to do so. If one is interested only in copy pasting, then no amount of explanation will help – David Soroko Dec 15 '14 at 14:28
  • 1
    Stackoverflow (and the SE network as whole) is not about helping that one user out there that just wants his code working... It's for all the others that have the same problem and find the SO question on google. Dont write your answer for the copy-paster but for the person coming from google, that wants some explanation with it. – Vogel612 Dec 15 '14 at 14:31
  • You are making this up: _"Stack Overflow is a question and answer site for professional and enthusiast programmers."_ as well as _"Good answers are voted up and rise to the top."_. Your down-vote is just noise. – David Soroko Dec 15 '14 at 14:39
  • 1
    Is it?? I think your answer is not a good answer as is, and thus downvoted it. Feel free to bring this up on [meta] or talk this out with me in [chat] (Java room). – Vogel612 Dec 15 '14 at 14:41
  • Off course, you are entitled to click the down button like anyone else. – David Soroko Dec 15 '14 at 14:42
-1

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
        }

    }
razcor
  • 355
  • 2
  • 11
  • Hiya, this may well solve the problem... but it'd be good if you could provide a little explanation about how and why it works :) Don't forget - there are heaps of newbies on Stack Overflow, and they could learn a thing or two from your expertise - what's obvious to you might not be so to them. – Vogel612 Dec 15 '14 at 14:22
  • 2
    Raw types are never a better approach. – Tom Dec 15 '14 at 14:22
  • Yep, I uderstand your comments, as i said, there are btter approaches, but if the OP is trying to solve in this way... – razcor Dec 15 '14 at 14:23
  • 3
    @razcor just because op wants to shoot himself in the leg, you don't really have to aim the gun at his crotch ;) instead you can just as well suggest a better approach – Vogel612 Dec 15 '14 at 14:24