Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

Different Ways Of Iterating Map In Java – Including forEach() and stream() Of Java 8

Posted on 01/14/2025 By admin

Map in Java is an interface. It stores data as key-value pair. We will learn different ways to iterate over Map including Java 8 stream() and forEach().

The Map is an interface in Java which can be used to store data as key-value pairs. If we want to store details of a person such as First name, last name, address, and age then we can use Map here from Java. First name, last name, address, and age are keys and the person’s details for these keys are values.

Since Map is an interface, we can not instantiate it. We need to use its implemented classes such as HashMap, TreeMap etc. Please note that the Map interface is not part of the Collection interface.

Below is a sample program –

package Maps; import java.util.HashMap;
import java.util.Map; public class WaysOfInteratingMap { public static void main(String[] args) { Map personData = new HashMap(); personData.put("FirstName", "Amod"); personData.put("LastName", "Mahajan"); personData.put("Address", "New York"); personData.put("Age", "30"); System.out.println(personData); }
}

Output

{Address=New York, FirstName=Amod, LastName=Mahajan, Age=30}

We can print the entire Map data i.e. key-value pair as shown above but in real-time we may be required to iterate the Map to perform different actions. In this post, we will see different ways of iterating over the Map including Java 8 methods.

Map interface provides a method called entrySet() which returns a Set view of the mappings contained in the map. Entry is an inner static interface of Map.

// Iterating using entrySet() method
Set> entries = personData.entrySet();
for(Entry entry : entries)
{ System.out.println(entry.getKey()+" : "+entry.getValue());
}
Address : New York
FirstName : Amod
LastName : Mahajan
Age : 30

The above method gives us key and value mappings but we can also get all keys using the keySet() method and iterate over it. This method returns a Set of all keys in Map.

// Iterating using keySet() method
Set allKeys = personData.keySet();
for(String key : allKeys)
{ System.out.println(key + " : "+personData.get(key));
}
Address : New York
FirstName : Amod
LastName : Mahajan
Age : 30

We can directly use the forEach() method of Map introduced in Java 8. With lambda, we can make it shorter.

// Iterating using forEach() method with lambda
personData.forEach((key,value) -> System.out.println(key + " : "+ value));
// Iterating using forEach() method without lambda
personData.forEach(new BiConsumer() { @Override public void accept(String key, String value) { System.out.println(key + " : "+ value); } });
Address : New York
FirstName : Amod
LastName : Mahajan
Age : 30

We can also use the stream() method which may be an indirect way of the above method. This method will be useful if you want to perform some actions on data.

// Iterating using stream() method
personData.keySet().stream().forEach((key) -> System.out.println(key + " : "+ personData.get(key)));
personData.entrySet().stream().forEach((keyValuePair) -> System.out.println(keyValuePair.getKey()+" : "+keyValuePair.getValue()));
 
Address : New York
FirstName : Amod
LastName : Mahajan
Age : 30
package Maps; import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Function; public class WaysOfInteratingMap { public static void main(String[] args) { Map personData = new HashMap(); personData.put("FirstName", "Amod"); personData.put("LastName", "Mahajan"); personData.put("Address", "New York"); personData.put("Age", "30"); System.out.println(personData); // Iterating using entrySet() method Set> entries = personData.entrySet(); for(Entry entry : entries) { System.out.println(entry.getKey()+" : "+entry.getValue()); } System.out.println("========================================"); // Iterating using keySet() method Set allKeys = personData.keySet(); for(String key : allKeys) { System.out.println(key + " : "+personData.get(key)); } System.out.println("========================================"); // Iterating using forEach() method with lambda personData.forEach((key,value) -> System.out.println(key + " : "+ value)); // Iterating using forEach() method without lambda personData.forEach(new BiConsumer() { @Override public void accept(String key, String value) { System.out.println(key + " : "+ value); } }); // Iterating using stream() method personData.keySet().stream().forEach((key) -> System.out.println(key + " : "+ personData.get(key))); System.out.println("========================================"); personData.entrySet().stream().forEach((keyValuePair) -> System.out.println(keyValuePair.getKey()+" : "+keyValuePair.getValue())); }
}

If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe to my YouTube channel.
#ThanksForReading
#HappyLearning

Uncategorized

Post navigation

Previous Post: getOrDefault
Next Post: Frequently Asked Java Program 30: Java Program to Find Distinct Characters In Given String

Related Posts

Selenium Topics – Page 11 – Make Selenium Easy Uncategorized
An Introduction to Selenium – A set of Tools Uncategorized
REST Assured Tutorial 49 – How To Retrieve Single and MultiValue Headers From Response Using Rest Assured Uncategorized
API Testing – Page 2 Uncategorized
fluentwait Uncategorized
REST Assured Tutorial 43 – Get All Keys From A Nested JSON Object Uncategorized

Recent Posts

  • Getting Started with Selenium 4: What Is New and How to Upgrade from Selenium 3
  • Manual Testing
  • Baby Steps To Become Efficient Selenium-Java Automation Tester
  • Features of Selenium 4.0.0 Release – Java Binding
  • Part 1: Handling Drop-down Created Using SELECT Tag In Selenium

Recent Comments

No comments to show.

Archives

  • April 2026
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • August 2023
  • November 2022
  • September 2022
  • August 2022
  • July 2022
  • May 2022
  • March 2022
  • October 2021
  • April 2021
  • March 2021
  • January 2021
  • December 2020
  • October 2020
  • September 2020
  • August 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • May 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • January 2018

Categories

  • Getting Started
  • Uncategorized

Copyright © 2026 Make Selenium Easy.

Powered by PressBook Masonry Dark