How to iterate Map in Java

In this tutorial, we are going to learn how to iterate a map in Java. As we know, we cannot iterate a map directly using iterators as a map is not a Collection. Firstly we have to convert the map into a set then we can iterate over it.

So, these are various ways of iterating a Map in Java: –

  1. Iterating using keySet() and values() method.
  2. Using For-Each loop over Map.entrySet().
  3. Using Map.forEach and lambda Function.
  4. Iterators over Map.Entry<>.

So, now we will explore each of the above methods one by one.

Iterate Map using keySet() and values() method Java

Map.values() returns the collection of each of the values present in the map. Whereas, Map.keySet() returns the set of keys in the map and now using for each loop to get the keys and the values.

Code: –

import java.util.*;
  
class Main  
{ 
    public static void main(String[] arg) 
    { 
        Map<String,Integer> mp = new HashMap<String,Integer>();  
        //Adding values to map
        mp.put("One",1); 
        mp.put("Two",2); 
        mp.put("Three",3); 
        mp.put("Four",4); 
        // Keyset for iteration over keys
        for (String num : mp.keySet())  
            System.out.println("Key Number: " + num); 
        // Iteration over values
        for (Integer inte : mp.values())  
            System.out.println("Corresponding Value: " + inte); 
    } 
}

Output: –

Key Number: One
Key Number: Four
Key Number: Two
Key Number: Three
Corresponding Value: 1
Corresponding Value: 4
Corresponding Value: 2
Corresponding Value: 3

So, this is how we use the keySet and values method for iteration.

Using For-Each loop over Map.entrySet()

This is one of the most widely used method. In this method we get both the key and the value using one loop. Map.entrySet() create a key-value pair and then using getKey() and getValue() we can extract key and the value.

Code: –

import java.util.*;
  
class Main  
{ 
    public static void main(String[] arg) 
    { 
        Map<String,Integer> mp = new HashMap<String,Integer>();  
        //Adding values to map
        mp.put("One",1); 
        mp.put("Two",2); 
        mp.put("Three",3); 
        mp.put("Four",4); 
        // Changing to entryset and iterating
        for (Map.Entry<String,Integer> it : mp.entrySet()) {
            System.out.println("Key : " + it.getKey() + ", Value : " + it.getValue());
        }
    } 
}

Output: –

Key : One, Value : 1
Key : Four, Value : 4
Key : Two, Value : 2
Key : Three, Value : 3

Using Map.forEach() and lambda Function

In this method, we will be using the lambda function in order to iterate through the key-value pair. This method is available from Java 8 onwards.

Code: –

import java.util.*;
  
class Main  
{ 
    public static void main(String[] arg) 
    { 
        Map<String,Integer> mp = new HashMap<String,Integer>();  
        //Adding values to map
        mp.put("One",1); 
        mp.put("Two",2); 
        mp.put("Three",3); 
        mp.put("Four",4); 
        // foreach iteration 
        mp.forEach((key,value) -> System.out.println("Key : "+ key + ", Value : " + value)); 
    } 
}

Output: –

Key : One, Value : 1
Key : Four, Value : 4
Key : Two, Value : 2
Key : Three, Value : 3

Iterate Using Iterators over Map.Entry<>

This method is almost the same as the second one but in this for iteration we use iterators. In the forme method we could only traverse but in this we can also remove values.

Code: –

import java.util.*;
  
class Main  
{ 
    public static void main(String[] arg) 
    { 
        Map<String,Integer> mp = new HashMap<String,Integer>();  
        //Adding values to map
        mp.put("One",1); 
        mp.put("Two",2); 
        mp.put("Three",3); 
        mp.put("Four",4); 
        // Iterators iteration 
        Iterator<Map.Entry<String, Integer>> it = mp.entrySet().iterator(); 
        while(it.hasNext()) 
        { 
             Map.Entry<String, Integer> pair = it.next(); 
             System.out.println("Key : " + pair.getKey() +  ", Value : " + pair.getValue()); 
        } 
    } 
}

Output: –

Key : One, Value : 1
Key : Four, Value : 4
Key : Two, Value : 2
Key : Three, Value : 3

In this method the order of printing is random.

So, these are the various ways by which we can iterate a Map in Java.

Also Read: –

Difference between HashMap and HashTable in Java

Leave a Reply