Hurry! Try our new Interactive Courses for FREE. 🥳   🚀

Iterate a Map in Java

The Map is an interface in Java that is used to store key-value pairs. We cannot directly iterate over a Map using iterators, as it is not a true collection. However, there are other ways of traversing a map. We will use the Map.Entry interface. It is used to store a single key-value mapping. We can apply the following techniques to iterate any map(HashMap, LinkedHashMap, or a TreeMap).

Using EntrySet and For Loop

The Map.entrySet() method returns a collection view(a Set) of our map, and each element of the returned set is of type Map.Entry. So we basically get a Set<Map.Entry<K, V>>. We can iterate through this Set and use the getKey() and getValue() methods to fetch the key and value from each Map.Entry. The following code shows its demonstration.

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class Demo
{
	public static void main(String[] args)
	{
		Map<Integer, String> map = new HashMap<>();
		map.put(101, "Justin");
		map.put(102, "Jessica");
		map.put(103, "Simon");
		map.put(104, "Harry");
		
		Set<Map.Entry<Integer, String>> entrySet = map.entrySet();
		
		for(Map.Entry<Integer, String> e : entrySet)
			System.out.println(e.getKey() + "--->" + e.getValue());
	}
}


101--->Justin
102--->Jessica
103--->Simon
104--->Harry

Using Iterator and EntrySet

In the above code, we used a for-loop. We can also traverse the Set returned by Map.entrySet() by using iterators. We will first get an Iterator instance using the iterator() method on the entry set. Then, we can use the hasNext() and next() methods to loop through each element and fetch it.

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

public class Demo
{
	public static void main(String[] args)
	{
		Map<Integer, String> map = new HashMap<>();
		map.put(101, "Justin");
		map.put(102, "Jessica");
		map.put(103, "Simon");
		map.put(104, "Harry");
		
		Set<Map.Entry<Integer, String>> entrySet = map.entrySet();
		Iterator<Entry<Integer, String>> entrySetIterator = entrySet.iterator();
		
		while(entrySetIterator.hasNext())
		{
			Map.Entry<Integer, String> e = entrySetIterator.next();
			Integer key = e.getKey();
			String value = e.getValue();
			System.out.println(key + "--->" + value);
		}
	}
}


101--->Justin
102--->Jessica
103--->Simon
104--->Harry

Using keySet() method of Map

As the name suggests, the Map.keySet() method returns a set containing all the keys of the map instance. We can iterate through each key and retrieve the corresponding value by using the get() method.

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class Demo
{
	public static void main(String[] args)
	{
		Map<Integer, String> map = new HashMap<>();
		map.put(101, "Justin");
		map.put(102, "Jessica");
		map.put(103, "Simon");
		map.put(104, "Harry");
		
		Set<Integer> keySet = map.keySet();
		
		for(Integer key : keySet)
			System.out.println(key + "--->" + map.get(key));
	}
}


101--->Justin
102--->Jessica
103--->Simon
104--->Harry

Using forEach() and Lambda Expressions(Java 8)

This is probably the simplest approach of all, and we can use this to iterate a Map in a single line of code. The code below demonstrates this technique.

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

public class Demo
{
	public static void main(String[] args)
	{
		Map<Integer, String> map = new HashMap<>();
		map.put(101, "Justin");
		map.put(102, "Jessica");
		map.put(103, "Simon");
		map.put(104, "Harry");
		
		map.forEach((key, value) -> System.out.println(key + "-->" + value));
	}
}


101-->Justin
102-->Jessica
103-->Simon
104-->Harry

Using Streams(Java 8)

We can use the Stream API to loop through a Map. We will again use the entrySet() method and then create a stream from this set. Next, we will use the forEach() method on each Map.Entry and fetch its key and value.

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class Demo
{
	public static void main(String[] args)
	{
		Map<Integer, String> map = new HashMap<>();
		map.put(101, "Justin");
		map.put(102, "Jessica");
		map.put(103, "Simon");
		map.put(104, "Harry");
		
		Set<Map.Entry<Integer, String>> entrySet = map.entrySet();
		
		entrySet.stream()
		   		.forEach(e -> System.out.println(e.getKey() + "-->" + e.getValue()));
	}
}


101-->Justin
102-->Jessica
103-->Simon
104-->Harry

Iterating Keys and Values Separately

In all the above sections, we were traversing the keys and values parallelly. However, we can iterate through the keys and values separately. We can get a set containing all the keys by using the Map.keySet() method. Then, we can simply use a for-loop to traverse the Set.

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class Demo
{
	public static void main(String[] args)
	{
		Map<Integer, String> map = new HashMap<>();
		map.put(101, "Justin");
		map.put(102, "Jessica");
		map.put(103, "Simon");
		map.put(104, "Harry");
		
		Set<Integer> keySet = map.keySet();
		
		for(Integer key : keySet)
			System.out.println(key);
	}
}


101
102
103
104

Likewise, we can use the values() method to fetch a Collection containing all the values of the map.

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

public class Demo
{
	public static void main(String[] args)
	{
		Map<Integer, String> map = new HashMap<>();
		map.put(101, "Justin");
		map.put(102, "Jessica");
		map.put(103, "Simon");
		map.put(104, "Harry");
		
		Collection<String> valueSet = map.values();
		
		for(String value : valueSet)
			System.out.println(value);
	}
}


Justin
Jessica
Simon
Harry

Summary

In this tutorial, we learned how to iterate a Map in Java. The Map.entrySet() is used in most of the approaches. This method returns a Set containing the map entries. We can then traverse through these entries by using a for-loop or an Iterator. We can also use the keySet() method to fetch the keys from the map. Use the values() method with a for-loop to iterate through the map values. We can also use the forEach() method with lambda expressions or streams to iterate a map, but they are available only in Java 8 and later versions.