Multiple ways to iterate Map and HashMap in Java with examples

This tutorials explains multiple ways to iterate key and values of a Map in java.

There are multiple ways to iterate in HashMap and Map in java

  • Iterator and Map.entrySet with java 4 version
  • using for loop
  • java8 foreach iterate with keySet,entrySet and values

Let’s declare an HashMap with Key of String and value of Integer.

Insert data into map using below code 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);

Map can be iterated using below methods

  • entrySet() method returns Map.entry object, that contains key and values.
  • keySet() method returns Set of key values.
  • values() method returns Collection of values

You can use one of the method using iteration approaches described below

Iterate key and value pairs in java map

This example uses Iterator and Map.Entry and It works in Java.

Iterator is used to iterate the collections types such as List,Set and Map

  • First, get Set of Map.Entry values using entrySet method, calls Iterator method
  • It returns Iterator<Map.Entry> object.
  • Next, Calls Iterator.hasNext method to check elements exists or not using while loop
  • print the values and using getKey and values method.
  • call next() method to move the iteration to next element.

This 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 for loop

Here is an example to iterate key 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 var keyword to declare variable without defining the type. Map.Entry type is not declared with type as var keywords allows you infer the type from the value.

  for (var pair : words.entrySet()) {
            System.out.println(pair.getKey() + ":" + pair.getValue());

        }
  • java8 foreach iterate with keySet,entrySet and values

java 8 introduced forEach method which takes lambda expression

Below ways of iterating pairs, key and values for a map

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