How To Iterate Map In Java Using Stream and Lambda?

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.

Using Lambda Functions with forEach()

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)));

Using Stream APIs

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)));

Complete Program

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)));

  }
}

Output

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.

Leave a Reply

Your email address will not be published. Required fields are marked *