Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

How To Iterate Map In Java Using Stream and Lambda?

Posted on 01/14/2025 By admin

We learned to Iterate a Map In Java using keySet(), entrySet(), values(), and iterator(). In this post, We will learn how can we use Java 8 features like Lambda functions and Stream APIs.

We can use forEach() method overridden in the HashSet class. It requires less code but you need to be aware of Lambda functions. You can refer YouTube playlist Learn Java Stream With Examples on my channel Retarget Common.

// Using forEach and lambda
mapData.forEach((key, value) -> System.out.println("Key = " + key + " | Value = " + value));

Using forEach() method you can directly access all keys and their associated values iteratively.

You can also call forEach() on the result of keySet(), entrySet(), and values() methods.

// Using keyset(), forEach and lambda
mapData.keySet().forEach((key) -> System.out.println("Key = " + key + " | Value = " + mapData.get(key)));

The Stream interface also provides a method forEach(). We can use that instead of HashMap’s forEach(). But you need to call that on the result of keySet(), entrySet(), and values() methods.

// Using keyset(), forEach and lambda
mapData.keySet().forEach((key) -> System.out.println("Key = " + key + " | Value = " + mapData.get(key)));

If you notice carefully, we are simply going an extra step to achieve whatever we have done above. But Stream API is really helpful if we want to perform some intermediate actions like filtering keys based on conditions.

In the below example, I am just filtering the key named “name”.

// Using stream, filter and forEach
mapData.keySet().stream()
  .filter(key -> !key.equals("name"))
  .forEach((key) -> System.out.println("Key = " + key + " | Value = " + mapData.get(key)));
package collectionsExamples; import java.util.*; public class IterateMapUsingLambdaStreams { public static void main(String[] args) { //create a map data HashMap mapData = new HashMap(); mapData.put("id", "1"); mapData.put("name", "Amod"); mapData.put("address", "New York"); mapData.put("skills", "Java Selenium"); mapData.put("mob", "969768"); // Using forEach and lambda mapData.forEach((key, value) -> System.out.println("Key = " + key + " | Value = " + value)); // Using keyset(), forEach and lambda mapData.keySet().forEach((key) -> System.out.println("Key = " + key + " | Value = " + mapData.get(key))); // Using stream and forEach mapData.keySet().stream().forEach((key) -> System.out.println("Key = " + key + " | Value = " + mapData.get(key))); // Using stream, filter and forEach mapData.keySet().stream() .filter(key -> !key.equals("name")) .forEach((key) -> System.out.println("Key = " + key + " | Value = " + mapData.get(key))); }
}

The output will be the same for the first three ways as below.

Key = skills | Value = Java Selenium
Key = mob | Value = 969768
Key = address | Value = New York
Key = name | Value = Amod
Key = id | Value = 1

The output of the last way is below.

Key = skills | Value = Java Selenium
Key = mob | Value = 969768
Key = address | Value = New York
Key = id | Value = 1

Thanks for reading.

Uncategorized

Post navigation

Previous Post: Plain Page Object Model In Selenium Webdriver (Without PageFactory)
Next Post: FluentWait in selenium webdriver

Related Posts

TestNG Tutorials 56: DataProvider in TestNG – Parameterizing DataProvider Method to Provide Data to Multiple Test Methods Uncategorized
September 30, 2018 – Make Selenium Easy Uncategorized
image – Make Selenium Easy Uncategorized
Hierarchy of Selenium Classes and Interfaces – Make Selenium Easy Uncategorized
Frequently Asked Java Program 08: Program to find occurrence of individual characters in a given string. Uncategorized
October 12, 2019 – Make Selenium Easy 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