Start learning free

LESSON 222 OF 671 · LOOPS WITH STRINGS, ARRAYS & DICTIONARIES

Looping Over a HashMap in Java with entrySet()

Lesson Read the full explanation

TL ; DR:

Loop over a map's entrySet() to visit every key with its value. Each entry has getKey() and getValue().


HashMap<String, Integer> stock = new HashMap<>();
stock.put("apples", 5);
stock.put("pears", 2);

for (Map.Entry<String, Integer> entry : stock.entrySet()) {
    System.out.println(entry.getKey() + " " + entry.getValue());
}

// pears 2
// apples 5   (in any order)


Full lesson:

A loop can visit every entry of a map, one at a time. The loop body runs once per key.

entrySet() hands the loop each entry of the map. An entry is a key with its value. Its type is Map.Entry<String, Integer>.

Loop over keySet() to get only the keys. You can still read each value with get.

for (String fruit : stock.keySet()) {
    System.out.println(fruit + " " + stock.get(fruit));
}

The order: a HashMap has no fixed order. The entries may come in any order. A total or a count comes out the same in any order. Those are safe answers.

Watch out: don't add or remove keys inside the loop. Java stops with a ConcurrentModificationException.


Your task

A game keeps each player's points in scores. The variables total and count start at 0.

Loop over scores. Add each entry's value to total. Count the players with 10 points or more in count.

The code at the bottom prints both.

Expected output:

43
2
STEP 1 OF 3
    Ctrl + Enter to check

    JAVA · LOOPS WITH STRINGS, ARRAYS & DICTIONARIES

    Looping Over Dictionaries in Java, explained

    What it is

    A HashMap stores key and value pairs. To work with every pair, you loop over the map. A for-each loop can't take the map itself, so you hand it one of the map's views. The most useful one is entrySet(), which gives one Map.Entry per pair. An entry holds a key and its value, and you read them with getKey() and getValue().

    HashMap<String, Integer> stock = new HashMap<>();
    stock.put("apples", 5);
    stock.put("pears", 2);
    
    for (Map.Entry<String, Integer> entry : stock.entrySet()) {
        System.out.println(entry.getKey() + " " + entry.getValue());
    }

    This prints:

    pears 2
    apples 5

    The entry's type is Map.Entry<String, Integer>: the same two type arguments as the map. The loop body runs once for every key.

    Looping over the keys only

    When you only need the keys, loop over keySet(). You can still read each value with get:

    for (String fruit : stock.keySet()) {
        System.out.println(fruit + " " + stock.get(fruit));
    }

    This prints the same two lines. The entrySet() form is usually better when you need both parts, because each entry already holds its value, so there is no second lookup.

    The order is not fixed

    A HashMap has no promised order. The entries can come in any order, and the order can change when the map grows. Do not write code that depends on which entry comes first. A total or a count comes out the same in any order, so those are safe things to compute in a loop.

    Don't change the keys inside the loop

    Adding or removing keys while a loop is running over the map breaks the loop. Java stops with a ConcurrentModificationException. For example, calling stock.put(fruit + "!", 1) inside the keySet() loop above throws it. Decide what to add or remove first, and change the map after the loop.

    The lesson's task

    The map scores holds four players' points: Ana 12, Ben 7, Cleo 15 and Dan 9. The task is to add up all the points in total and count the players with 10 points or more in count. Both variables start at 0.

    for (Map.Entry<String, Integer> entry : scores.entrySet()) {
        total += entry.getValue();
        if (entry.getValue() >= 10) {
            count += 1;
        }
    }

    The three steps are: write the loop with entry as its variable, add entry.getValue() to total, and, below that, add 1 to count when the value is at least 10. With the lesson's code printing both variables afterwards, the output is:

    43
    2

    12 + 7 + 15 + 9 is 43, and only Ana (12) and Cleo (15) reach 10.

    Working through the loop

    On each turn of the loop, entry is a different pair of the map. On the turn for Ana, entry.getKey() is "Ana" and entry.getValue() is 12, so total grows by 12 and count grows by 1. On the turn for Ben, the value is 7, so total grows by 7 and count stays the same. Whichever order the four players arrive in, the final total and count are the same. That is why these two answers are safe to compute from a HashMap.

    Common mistakes

    • Using int as the loop variable's type. An entry is an object, so the variable is a Map.Entry, not a number.
    • Calling entry.getValue() when you wanted the key, or the other way round.
    • Expecting the entries in the order you added them. For that you would need a different map class.
    • Adding or removing keys inside the loop.

    Key takeaways

    • for (Map.Entry<K, V> entry : map.entrySet()) visits every pair once.
    • getKey() and getValue() read the two parts of an entry.
    • keySet() gives only the keys.
    • Rely on totals and counts, not on the order.
    Keep going with Java500+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free