Different way to iterate Map in Java

Connect with

Oracle Java
There are different way of traversal of Map HashMap , ConcurrentHashMap object in java for getting key/value pair . In day-to-day life, we almost use Map (either get or put operation) on every alternate day in java programming language. But way of iteration is different , its depend upon the requirement and thing in mind on that point of time. Most of the time while iteration we don’t know the key and its value. we have to iterate over and best on that we usually process.

Using JDK 5 for-each with EntrySet

This is simplest of all and elegant of all the iteration, developer used this way very frequently.


// Using JDK 5 for-each with EntrySet
System.out.println("Using JDK 5 for-each with EntrySet...");
for (Map.Entry entry : countryMap.entrySet()) {
   System.out.printf("Key : %s and Value: %s \n ", entry.getKey(), entry.getValue());
}

Using for-each loop with keySet()


// Using for-each loop with keySet()
System.out.println("Using for-each loop with keySet()........");
for (String key : countryMap.keySet()) {
   System.out.printf(" key : %s and value: %s \n", key, countryMap.get(key));
}

Using while loop with entrySet()

// Using  while loop with entrySet()
System.out.println("Using  while loop with entrySet()..............");
Iterator> itr = countryMap.entrySet().iterator();
while(itr.hasNext()) {
  Map.Entry entry = itr.next();
  System.out.printf(" key : %s and value: %s \n",  entry.getKey(), entry.getValue());
}

Using while loop with keySet()


// Using while loop with keySet()
System.out.println("Using while loop with keySet()............................");
Iterator itr2 = countryMap.keySet().iterator();
while (itr2.hasNext()) {
  String key = itr2.next();
  System.out.printf(" key : %s and value: %s \n",  key, countryMap.get(key));
}

Complete Working Example to iterate Over Map in different ways


package com.mysoftkey.collection;

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

/**
 * This java class is used to demonstrate the 
 * different way to iterate Map or HashMap in java.
 * 
 * @author ranjeet.jha
 *
 */
public class MapIteratorExample {

  public static void main(String[] args) {
		
	 // creating a HashMap object and add few key/value in it.
	 Map countryMap = new HashMap();
	 countryMap.put("IN", "India");
	 countryMap.put("USA", "United States of America");
	 countryMap.put("SG", "Singapore");
	 countryMap.put("CA", "Canada");

	// Using JDK 5 for-each with EntrySet
	System.out.println("Using JDK 5 for-each with EntrySet...");
        for (Map.Entry entry : countryMap.entrySet()) {
            System.out.printf("Key : %s and Value: %s \n", entry.getKey(), entry.getValue());
        }
		
        // Using for-each loop with keySet()
        System.out.println("Using for-each loop with keySet()........");
        for (String key : countryMap.keySet()) {
           System.out.printf(" key : %s and value: %s \n ", key, countryMap.get(key));
        }
		
        // Using  while loop with entrySet()
        System.out.println("Using  while loop with entrySet()..............");
        Iterator> itr = countryMap.entrySet().iterator();
        while(itr.hasNext()) {
           Map.Entry entry = itr.next();
           System.out.printf(" key : %s and value: %s \n ",  entry.getKey(), entry.getValue());
        }
        
        // Using while loop with keySet()
        System.out.println("Using while loop with keySet()............................");
        Iterator itr2 = countryMap.keySet().iterator();
	  while (itr2.hasNext()) {
		String key = itr2.next();
		System.out.printf(" key : %s and value: %s  \n",  key, countryMap.get(key));
	  }
        
	}

}

In the above example you can create LinkedHashMap, ConcurrentHashMap or Collections.synchronizedMap(Map), but iteration remain same, only object of the implementation gets changed.

Output

Using JDK 5 for-each with EntrySet...
Key : USA and Value: United States of America 
Key : SG and Value: Singapore 
Key : IN and Value: India 
Key : CA and Value: Canada 
Using for-each loop with keySet()........
 key : USA and value: United States of America 
 key : SG and value: Singapore 
 key : IN and value: India 
 key : CA and value: Canada 
Using  while loop with entrySet()..............
 key : USA and value: United States of America 
 key : SG and value: Singapore 
 key : IN and value: India 
 key : CA and value: Canada 
Using while loop with keySet()............................
 key : USA and value: United States of America 
 key : SG and value: Singapore 
 key : IN and value: India 
 key : CA and value: Canada 

Best way to iterate over Map/HashMap

In the above jdk5 for-each with EntrySet is the best way to iterate . this is simple and elegant way of iteration over HashMap or any Map


// Using JDK 5 for-each with EntrySet
System.out.println("Using JDK 5 for-each with EntrySet...");
for (Map.Entry entry : countryMap.entrySet()) {
   System.out.printf("Key : %s and Value: %s \n", entry.getKey(), entry.getValue());
}

Suggestions are welcome to improve this post. cheers happy learning 🙂


Connect with

2 thoughts on “Different way to iterate Map in Java

  1. Pingback: andy
  2. Pingback: kumud

Leave a Reply

Your email address will not be published. Required fields are marked *