In one of the previous article, we have discussed various ways to iterate through Map i.e.;
Ways to iterate through Map:
- Using keySet() method and for-each loop
- Using keySet() method and Iterator interface
- Using entrySet() method and for-each loop
- Using entrySet() method and Iterator interface
- Read different ways to iterate Map Entry
We will revisit examples for iterating through Map objects prior to Java 1.7 version and finally iterating Map object using enhanced for-each loop introduced in Java 1.8 version
- get key-set using keySet() method of Map interface and iterate using for-each loop
- get entry-set using entrySet() method of Map interface and iterate using for-each loop
- Iterating Map object using for-each loop introduced in Java 1.8 version
1. Get keySet() and iterate using enhanced for-each loop
- With enhanced for-loop and keySet() method, we can iterate through Map as demonstrated below,
IteratingMapUsingKeySetAndEnhancedForLoop.java
package in.bench.resources.java.collection.list;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class IteratingMapUsingKeySetAndEnhancedForLoop {
public static void main(String[] args) {
// creating HashMap object of type <String, String>
Map<String, String> hm = new HashMap<String, String>();
// adding key-value pairs to HashMap object
hm.put("Google", "Sundar Pichai");
hm.put("Facebook", "Mark Zuckerberg");
hm.put("LinkedIn", "Reid Hoffman");
hm.put("Apple", "Steve Jobs");
hm.put("Microsoft", "Bill Gates");
// Get keySet() and Iterate using for-each loop
System.out.println("Get keySet() and "
+ "Iterate using for-each loop\n");
// getting keySet() into Set
Set<String> set1 = hm.keySet();
// for-each loop
for(String key : set1) {
System.out.println("Org : " + key + "\t\t"
+ "CEO : " + hm.get(key));
}
}
}
Output:
Get keySet() and Iterate using for-each loop
Org : Google CEO : Sundar Pichai
Org : LinkedIn CEO : Reid Hoffman
Org : Apple CEO : Steve Jobs
Org : Microsoft CEO : Bill Gates
Org : Facebook CEO : Mark Zuckerberg
2. Get entrySet() and iterate using enhanced for-each loop
- With enhanced for-loop and entrySet() method, we can iterate through Map as demonstrated below,
IteratingMapUsingEntrySetAndEnhancedForLoop.java
package in.bench.resources.java.collection.list;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
public class IteratingMapUsingEntrySetAndEnhancedForLoop {
public static void main(String[] args) {
// creating HashMap object of type <String, String>
Map<String, String> hm = new HashMap<String, String>();
// adding key-value pairs to HashMap object
hm.put("Google", "Sundar Pichai");
hm.put("Facebook", "Mark Zuckerberg");
hm.put("LinkedIn", "Reid Hoffman");
hm.put("Apple", "Steve Jobs");
hm.put("Microsoft", "Bill Gates");
// get entrySet() and Iterate using for-each loop
System.out.println("Get entrySet() and "
+ "Iterate using for-each loop\n");
// getting entrySet() into Set
Set<Entry<String, String>> entrySet1 = hm.entrySet();
// for-each loop
for(Entry<String, String> entry1 : entrySet1) {
System.out.println("Org : " + entry1.getKey() + "\t\t"
+ "CEO : " + entry1.getValue());
}
}
}
Output:
Get entrySet() and Iterate using for-each loop
Org : Google CEO : Sundar Pichai
Org : LinkedIn CEO : Reid Hoffman
Org : Apple CEO : Steve Jobs
Org : Microsoft CEO : Bill Gates
Org : Facebook CEO : Mark Zuckerberg
3. Iterate Map objects using for-each loop in Java 8 version
- In Java 8, we can iterate Map using Iterable.forEach() loop
IteratingMapUsingForEachLoopInJava8.java
package in.bench.resources.java.collection.list;
import java.util.HashMap;
import java.util.Map;
public class IteratingMapUsingForEachLoopInJava8 {
public static void main(String[] args) {
// creating HashMap object of type <String, String>
Map<String, String> companyCEO = new HashMap<String, String>();
// adding key-value pairs to HashMap object
companyCEO.put("Google", "Sundar Pichai");
companyCEO.put("Facebook", "Mark Zuckerberg");
companyCEO.put("LinkedIn", "Reid Hoffman");
companyCEO.put("Apple", "Steve Jobs");
companyCEO.put("Microsoft", "Bill Gates");
// Iterating Map using forEach() in Java 8
System.out.println("Iterating Map using forEach() in Java 8\n");
companyCEO.forEach((key, value)->System.out.println(
"Org : " + key + "\t\t" + "CEO : " + value));
}
}
Output:
Iterating Map using forEach() in Java 8
Org : Google CEO : Sundar Pichai
Org : LinkedIn CEO : Reid Hoffman
Org : Apple CEO : Steve Jobs
Org : Microsoft CEO : Bill Gates
Org : Facebook CEO : Mark Zuckerberg
From above example, HashMap
- allows only unique keys to be stored
- null object is allowed; but maximum of one
- while iterating, keys are retrieved in random-order
To conclude, now there are 5 ways to iterate Map Entries
- Using keySet() method and for-each loop
- Using keySet() method and Iterator interface
- Using entrySet() method and for-each loop
- Using entrySet() method and Iterator interface
- Iterating Map using forEach() in Java 8
References:
- https://docs.oracle.com/javase/tutorial/collections/intro/
- https://docs.oracle.com/javase/tutorial/collections/interfaces/collection.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Collection.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Map.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Map.Entry.html
- https://docs.oracle.com/javase/tutorial/collections/interfaces/map.html
- https://docs.oracle.com/javase/7/docs/api/java/util/HashMap.html
- https://docs.oracle.com/javase/8/docs/api/java/lang/Iterable.html#forEach-java.util.function.Consumer-
- https://docs.oracle.com/javase/8/docs/technotes/guides/language/foreach.html
- https://docs.oracle.com/javase/8/docs/api/java/util/Iterator.html
Happy Coding !!
Happy Learning !!