Java HashMap forEach() Method

The HashMap.forEach() method in Java is used to perform the given action for each entry 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. forEach Method Syntax
  3. Examples
    • Iterating Over Entries in a HashMap
    • Real-World Use Case: Printing Employee Details
  4. Conclusion

Introduction

The HashMap.forEach() method is a member of the HashMap class in Java. It allows you to perform a specified action for each entry in the map. This can be useful for iterating over the map's entries and applying a common operation to each key-value pair.

forEach() Method Syntax

The syntax for the forEach method is as follows:

public void forEach(BiConsumer<? super K, ? super V> action)
  • The method takes a single parameter:
    • action of type BiConsumer<? super K, ? super V>, which represents the action to be performed for each entry in the map.
  • The method does not return any value.

Examples

Iterating Over Entries in a HashMap

The forEach method can be used to iterate over the entries in a HashMap and perform an action on each entry.

Example with Lambda Expression

import java.util.HashMap;

public class ForEachExample {
    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);

        // Using forEach method with a lambda expression to print each entry
        people.forEach((key, value) -> System.out.println("Key: " + key + ", Value: " + value));
    }
}

Output:

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

Real-World Use Case: Printing Employee Details

In a real-world scenario, you might use the forEach method to print the details of employees stored in a HashMap.

Example with Lambda Expression

import java.util.HashMap;

public class PrintEmployeeDetails {
    public static void main(String[] args) {
        // Creating a HashMap with String keys (employee IDs) and String values (employee names)
        HashMap<String, String> employeeDatabase = new HashMap<>();

        // Adding entries to the HashMap
        employeeDatabase.put("E001", "Ravi Kumar");
        employeeDatabase.put("E002", "Priya Sharma");
        employeeDatabase.put("E003", "Vijay Singh");

        // Using forEach method with a lambda expression to print each employee's details
        employeeDatabase.forEach((key, value) -> System.out.println("Employee ID: " + key + ", Employee Name: " + value));
    }
}

Output:

Employee ID: E001, Employee Name: Ravi Kumar
Employee ID: E002, Employee Name: Priya Sharma
Employee ID: E003, Employee Name: Vijay Singh

Conclusion

The HashMap.forEach() method in Java provides a way to perform a specified action for each entry in the HashMap. Using lambda expressions with this method makes the code more concise and readable. By understanding how to use this method, you can efficiently iterate over the map's entries and apply operations to each key-value pair. This method is useful in various scenarios, such as printing data, updating values, and performing calculations based on map entries.

Comments