Java HashMap entrySet().stream() Method

The HashMap.entrySet().stream() method in Java is used to create a Stream of the entries contained in the HashMap. This guide will cover the method's usage, explain how it works, and provide examples to demonstrate its functionality.

Table of Contents

  1. Introduction
  2. entrySet().stream() Method Syntax
  3. Examples
    • Using entrySet().stream() to Iterate Over Entries
    • Real-World Use Case: Filtering Entries Based on a Condition
  4. Conclusion

Introduction

The HashMap.entrySet().stream() method is a combination of HashMap.entrySet() and Set.stream(). It provides a Stream of the entries (key-value pairs) contained in the HashMap. A Stream is a sequence of elements supporting sequential and parallel aggregate operations. This method can be useful for performing complex operations on the entries of a HashMap.

entrySet().stream() Method Syntax

The syntax for obtaining a Stream of entries from a HashMap is as follows:

hashMap.entrySet().stream()
  • The method does not take any parameters.
  • The method returns a Stream of the entries in the HashMap.

Examples

Using entrySet().stream() to Iterate Over Entries

The entrySet().stream() method can be used to create a Stream for iterating over the entries in a HashMap.

Example

import java.util.HashMap;
import java.util.Map;
import java.util.stream.Stream;

public class EntryStreamExample {
    public static void main(String[] args) {
        // Creating a HashMap with String keys and Integer values
        HashMap<String, Integer> people = new HashMap<>();

        // Adding entries to the HashMap
        people.put("Ravi", 25);
        people.put("Priya", 30);
        people.put("Vijay", 35);

        // Getting the entry stream
        Stream<Map.Entry<String, Integer>> entryStream = people.entrySet().stream();

        // Using the entry stream to iterate over the entries
        entryStream.forEach(entry -> System.out.println("Key: " + entry.getKey() + ", Value: " + entry.getValue()));
    }
}

Output:

Key: Ravi, Value: 25
Key: Priya, Value: 30
Key: Vijay, Value: 35

Real-World Use Case: Filtering Entries Based on a Condition

In a real-world scenario, you might use the entrySet().stream() method to filter entries based on a certain condition, such as finding entries where the value is greater than a specific number.

Example

import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class FilterEntriesExample {
    public static void main(String[] args) {
        // Creating a HashMap with String keys and Integer values
        HashMap<String, Integer> people = new HashMap<>();

        // Adding entries to the HashMap
        people.put("Ravi", 25);
        people.put("Priya", 30);
        people.put("Vijay", 35);
        people.put("Rohit", 40);

        // Getting the entry stream
        Stream<Map.Entry<String, Integer>> entryStream = people.entrySet().stream();

        // Filtering entries where the value is greater than 30
        Stream<Map.Entry<String, Integer>> filteredEntries = entryStream.filter(entry -> entry.getValue() > 30);

        // Collecting the filtered entries into a list and printing them
        Map<String, Integer> filteredMap = filteredEntries.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
        System.out.println("Entries with values greater than 30: " + filteredMap);
    }
}

Output:

Entries with values greater than 30: {Rohit=40, Vijay=35}

Conclusion

The HashMap.entrySet().stream() method in Java provides a way to create a Stream of the entries contained in the HashMap. By understanding how to use this method, you can efficiently perform complex operations on the entries, such as filtering, mapping, and reducing. This method is useful in various scenarios, such as iterating over entries, filtering entries based on conditions, and performing aggregate operations on entries in a HashMap.

Comments