Multiple ways to iterate Map and HashMap in Java with examples

This tutorial explains multiple ways to iterate over keys and values of a Map in Java.

There are various methods to iterate in a HashMap and Map in Java.

  • Using Iterator and Map.entrySet with Java 4 version.
  • Using a for loop.
  • Java 8 forEach iteration with keySet, entrySet, and values.

Let’s declare a HashMap with keys of type String and values of type Integer.

Insert data into the map using the code below, for example:

HashMap<String, Integer> words = new HashMap<>();

words.put("one", 1);
words.put("two", 2);
words.put("three", 3);
words.put("four", 4);
words.put("five", 5);
words.put("six", 6);

A map can be iterated using the following methods:

  • entrySet() method returns a Map.Entry object that contains keys and values.
  • keySet() method returns a set of key values.
  • values() method returns a collection of values.

You can use one of the following methods using the iteration approaches described below.

Iterate over key and value pairs in Java map

This example uses Iterator and Map.Entry to loop key and values in Java.

Iterator is used to iterate over collection types such as List, Set, and Map.

  • First, get a set of Map.Entry values using the entrySet method, call Iterator method.
  • It returns an Iterator<Map.Entry> object.
  • Next, call the Iterator.hasNext method to check if elements exist or not using a while loop.
  • Print the values using getKey and getValue methods.
  • Call next() method to move the iteration to the next element.

Here is an example:

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class MapIteration {
    public static void main(String[] args) {
        HashMap<String, Integer> words = new HashMap<>();

        words.put("one", 1);
        words.put("two", 2);
        words.put("three", 3);
        words.put("four", 4);
        words.put("five", 5);
        words.put("six", 6);

        Iterator<Map.Entry<String, Integer>> iterator = words.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, Integer> keyvalues = iterator.next();
            System.out.println(keyvalues.getKey() + ":" + keyvalues.getValue());

        }
    }
}
  • Using a for loop

    Here is an example to iterate over keys and values in Java

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

    In Java 10 version, you can use the var keyword to declare a variable without defining the type. The Map.Entry type is not declared, as var allows you to infer the type from the value.

    for (var pair : words.entrySet()) {
        System.out.println(pair.getKey() + ":" + pair.getValue());
    }
    
  • Java 8 forEach iteration with keySet, entrySet, and values:

    Java 8 introduced the forEach method, which takes a lambda expression.

    Below are ways of iterating over pairs, keys, and values for a map:

    // iterate over key and values
    words.entrySet().forEach(entry -> System.out.println(entry));
    // iterate over keys
    words.keySet().forEach(key -> System.out.println(key));
    // iterate over values
    words.values().forEach(value -> System.out.println(value));