Java 8 – Iterating Map using forEach() method

In one of the previous article, we have discussed various ways to iterate through Map i.e.;

Ways to iterate through Map:

  1. Using keySet() method and for-each loop
  2. Using keySet() method and Iterator interface
  3. Using entrySet() method and for-each loop
  4. Using entrySet() method and Iterator interface
  5. 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

  1. get key-set using keySet() method of Map interface and iterate using for-each loop
  2. get entry-set using entrySet() method of Map interface and iterate using for-each loop
  3. 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

References:

Happy Coding !!
Happy Learning !!

Java - How to delete an entry of HashMap ?
Java - Iterate through HashMap in 5 ways