Learn different ways to iterate Map in Java using for-each loop keySet, entrySet, for loop, while loop.
1. Ways to iterate Map in Java
There are different ways 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) every day in a java programming language. But the way of iteration is different, it depends upon the requirement and thing in mind at that point in 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.
2. Traversing Using JDK 5 for-each with EntrySet
This is simplest of all and elegant of all the iteration, developer used this way very frequently. This is one of the way of traversing of map using for-each loop with EntrySet.
// Using JDK 5 for-each with EntrySet System.out.println("Using JDK 5 for-each with EntrySet..."); for (Map.Entry<String, String> entry : countryMap.entrySet()) { System.out.printf("Key : %s and Value: %s \n ", entry.getKey(), entry.getValue()); }
3. Iteration Using for-each loop with keySet()
In this section we learn by using for-each loop with keySt, this is one of the ways to iterate Map in Java.
// 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)); }
4. Using while loop with entrySet()
In this section while loop has been used with entrySet() to iterate Map in Java.
// 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()); }
5. Traversing Using while loop with keySet()
// Using while loop with keySet() System.out.println("Using while loop with keySet()............................"); Iteratoritr2 = countryMap.keySet().iterator(); while (itr2.hasNext()) { String key = itr2.next(); System.out.printf(" key : %s and value: %s \n", key, countryMap.get(key)); }
6. 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; public class MapIteratorExample { public static void main(String[] args) { // creating a HashMap object and add few key/value in it. MapcountryMap = 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 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.
7. Output of Iterate element in Map
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
8. 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<String, String> entry : countryMap.entrySet()) { System.out.printf("Key: %s and Value: %s \n", entry.getKey(), entry.getValue()); }
9. Reference
Visit Oracle Java official Site for more details.
I hope enjoyed different ways to iterate java.util.Map
in Java using for-each keySet, for each entrySet, whi visit Core Java tutorial for more details.
Suggestions are welcome, to improve this topic, please write a comment for your thought.
Pingback: andy
Pingback: kumud