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.

Answer from Tom on Stack Overflow
๐ŸŒ
Netjstech
netjstech.com โ€บ 2015 โ€บ 07 โ€บ how-to-iterate-hash-map-of-arraylists-java.html
How to Iterate a HashMap of ArrayLists of String in Java | Tech Tutorials
June 27, 2021 - In the second for-each loop List that is retrieved using listEntry.getValue() is iterated and the elements that are in the list are displayed. import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class MapLoop { public static void main(String[] args) { MapLoop mapLoop = new MapLoop(); Map<String, List<String>> cityMap = mapLoop.getMap(); int i = 0; // iterating over a map for(Map.Entry<String, List<String>> listEntry : cityMap.entrySet()){ System.out.println("Iterating list number - " + ++i); // iterating over a list for(String cityName : li
Discussions

java - How to iterate HashMap<String, ArrayList<Car>> - Stack Overflow
Map> cars = new HashMap>; ArrayList carList = cars.get("bmw"); for (int i = 0; i < carList.size(); i++) { System.out.println(carList.get(i)); } ... Despite the "bmw" key exists and is populated. ... Much better than iterator is the enhanced ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
java - How to iterate Hashmap with containing arraylist - Stack Overflow
Possible Duplicate: How do I iterate over each Entry in a Map? i have map like HashMap (); i... More on stackoverflow.com
๐ŸŒ stackoverflow.com
May 22, 2017
java - How to iterate Arraylist<HashMap<String,String>>? - Stack Overflow
I have an ArrayList object like this: ArrayList (); How to iterate through the list? I want to displa... More on stackoverflow.com
๐ŸŒ stackoverflow.com
October 7, 2011
Loop through an ArrayList of HashMaps Java - Stack Overflow
Also you might want to change how you declare myList : List> myList = new ArrayList>(); 2013-04-12T20:00:14.043Z+00:00 ... @vtheron If it's just List, then Iterator should be used instead of direct indexes; if List happens to be LinkedList, indexing ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
Benchresources
benchresources.net โ€บ home โ€บ java โ€บ java โ€“ different ways to iterate over hashmap of arraylist
Java - Different ways to iterate over HashMap of ArrayList - BenchResources.Net
September 28, 2022 - package in.bench.resources.iterating.hashmap.of.arraylist; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Set; public class IteratingHashMapUsingKeySetAndForLoop { public static void main(String[] args) { // create HashMap of Continent &amp; list of Top Countries Map<String, List<String>> continentTopContries = new HashMap<String, List<String>>(); // create ArrayList-1 // for adding top countries of Asian Continent List<String> topCountriesOfAsiaContinent = new ArrayList<String>(); // add top countries of Asian continent topC
๐ŸŒ
Quora
quora.com โ€บ How-do-you-iterate-over-a-Hash-map-of-arraylists-of-String-in-Java
How to iterate over a Hash map of arraylists of String in Java - Quora
Answer (1 of 3): This can be done quite compactly in Java 8+: [code]map.values().stream().flatMap(List::stream).forEach(System.out::println) [/code]This will flatten a [code ]Map [/code] , and print the result out to the console. You can run the following test to verif...
๐ŸŒ
Blogger
javahungry.blogspot.com โ€บ 2021 โ€บ 02 โ€บ iterate-hashmap-with-arraylist.html
How to Iterate HashMap with ArrayList in Java with Example | Java Hungry
import java.util.*; public class HashMapWithArrayList { public static void main(String args[]) { // Creating HashMap object with ArrayList as Value HashMap<String, ArrayList<String>> map = new HashMap<>(); // Inserting key-value pairs in the above map map.put("USA", new ArrayList(Arrays.asList("Boston","NewYork","San-Francisco"))); map.put("INDIA", new ArrayList(Arrays.asList("Bangalore","Mumbai","Delhi"))); map.put("UK", new ArrayList(Arrays.asList("Leicester","London","Birmingham"))); // Iterating the Map for(Map.Entry<String, ArrayList<String>> entry : map.entrySet()){ // Use entry.getKey() to fetch Key from entry object System.out.println("Iterating cities of the country: "+ entry.getKey()); // Iterating the ArrayList
Find elsewhere
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ how-to-iterate-hashmap-in-java
How to Iterate HashMap in Java? - GeeksforGeeks
July 23, 2025 - // Java Program to Iterate over HashMap // Importing Map and HashMap classes // from package names java.util import java.util.HashMap; import java.util.Map; // Class for iterating HashMap using for loop public class GFG { // Main driver method public static void main(String[] args) { // Creating a HashMap Map<String, String> foodTable = new HashMap<String, String>(); // Inserting elements to the adobe HashMap // Elements- Key value pairs using put() method foodTable.put("A", "Angular"); foodTable.put("J", "Java"); foodTable.put("P", "Python"); foodTable.put("H", "Hibernate"); // Iterating HashMap through for loop for (Map.Entry<String, String> set : foodTable.entrySet()) { // Printing all elements of a Map System.out.println(set.getKey() + " = " + set.getValue()); } } }
๐ŸŒ
ZetCode
zetcode.com โ€บ java โ€บ hashmapiterate
Java HashMap iteration - learn how to iterate HashMap in Java
Each entry has a key string and list value. String key = me.getKey(); We get the key with getKey method. List<String> values = me.getValue(); We get the list with getValue. for (String e : values) { System.out.printf("%s ", e); } In the inner for loop, we iterate over the list of values.
Top answer
1 of 3
1

It doesn't matter whether you use a for loop, forEach or the Stream API. In all cases, you are iterating over a Map to compare each key against a certain value, which is perverting the concept of maps, to associate the key with a value and provide (usually far better that linear) lookup methods.

Further, you should use a Map<String, List<String>> instead, not referring to an implementation type like ArrayList, and not letting it be null in the first place, instead of having it to check for null later-on.

If you follow theses advice, your code becomes

Map<String, List<String>> xhashMap;
// always initialize the map to a non-null reference

xhashMap.getOrDefault("ax", Collections.emptyList())
        .forEach(v -> method1(v, AA.class));
xhashMap.getOrDefault("bx", Collections.emptyList())
        .forEach(v -> method1(v, BB.class));

If the map is, as the variable name suggests, a hash map, the two lookups will have O(1) time complexity, but even a TreeMap with O(log(n)) complexity will be better than iterating over the map and compare all keys.

As long as the action consists of a sole method invocation with different parameters, there is not much gain in trying to re-use common code, as the sharing code would be much more complicated.

2 of 3
1

Yes, we can't write it with Stream API, but it's not much better.

Since you are performing side effects and not collecting results, Stream would essentially be:

xhashMap.entrySet()
  .stream()
  .forEach(e -> ...);

and unfortunately, contain same logic inside the forEach.

Actually, you can even skip Stream creation at this point because you can perform forEach without creating a Stream:

xhashMap.entrySet()
  .forEach(e -> ...);
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ convert-arraylist-to-hashmap-in-java
Convert ArrayList to HashMap in Java - GeeksforGeeks
July 23, 2025 - Here, we just need to iterate on each of the elements of the ArrayList and the element can be converted into the key-value pair and store in the HashMap. ... // Java program to convert ArrayList // to HashMap import java.util.ArrayList; import ...
๐ŸŒ
Programiz
programiz.com โ€บ java-programming โ€บ examples โ€บ iterate-over-hashmap
Java Program to Iterate over a HashMap
import java.util.HashMap; import java.util.Iterator; import java.util.Map.Entry; class Main { public static void main(String[] args) { // create a HashMap HashMap<String, String> languages = new HashMap<>(); languages.put("Java", "Enterprise"); languages.put("Python", "ML/AI"); languages.put("JavaScript", "Frontend"); System.out.println("HashMap: " + languages); // create an object of Iterator Iterator<Entry<String, String>> iterate1 = languages.entrySet().iterator(); // iterate through key/value mappings System.out.print("Entries: "); while(iterate1.hasNext()) { System.out.print(iterate1.next
๐ŸŒ
Sentry
sentry.io โ€บ sentry answers โ€บ java โ€บ how to iterate over a hashmap in java
How to Iterate Over a HashMap in Java | Sentry
Perhaps the most straightforward approach to iterating over a HashMap is to use a for-each loop to iterate over each entry. Using the HashMap.entrySet() will return a set view of the mappings or entries in the HashMap. import java.util.HashMap; ...
๐ŸŒ
Reddit
reddit.com โ€บ r/learnjava โ€บ hashmap how to get values from the list value?
r/learnjava on Reddit: HashMap<String, ArrayList> How to get values from the list value?
December 21, 2018 -

Really stumped on this one. I have a HashMap object and the values are a list. I have no idea how to access the individual items in the list values.

Something like this:

public HashMap<String, ArrayList> getData() 
{
    HashMap<String, ArrayList> testData = new HashMap<String, ArrayList>();
    ArrayList<String> stringList = new ArrayList<String>();
    ArrayList<Integer> integerList = new ArrayList<Integer>();
    
    int testInt1 = 8;
    int testInt2 = 12;
    
    String testString1 = "Hello";
    String testString2 = "World";
    
    testData.put("integers", integerList);
    testData.put("strings", stringList);
    
    testData.get("integers").add(testInt1);
    testData.get("integers").add(testInt2);
    testData.get("strings").add(testString1);
    testData.get("strings").add(testString2);
    
    return testData;
}

The HashMap is being returned back to me and I want to access each individual item in each list.

๐ŸŒ
Reddit
reddit.com โ€บ r/learnjava โ€บ how to iterate over hashmap in a for-loop context?
r/learnjava on Reddit: How To Iterate Over Hashmap In A For-Loop Context?
July 20, 2023 -

I have a hashmap where each set's key is an Integer and the value is a custom inner class called node.

My for loop currently looks like this (hm is the variable that holds the hashmap):

for(int x=0; x<hm.entrySet().size(); x++){

    HashMap.Entry<Integer, node> currSet = hm.entrySet();
}

I know I need to do something to the " hm.entrySet() " inside the for-loop in order to access the set which is at index x. How do I do that?

Ideally I would have liked " hm.entrySet().get(x) " (the same syntax as accessing an arraylist) but that doesn't work.

What is the equivalent of " get(x) " in a situation like this?