The HashMap.forEach()
method in Java is used to perform the given action for each entry in the HashMap
.
Table of Contents
- Introduction
forEach
Method Syntax- Examples
- Iterating Over Entries in a HashMap
- Real-World Use Case: Printing Employee Details
- 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 typeBiConsumer<? 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;
import java.util.Map;
public class ForEachExample {
public static void main(String[] args) {
// Creating a Map with String keys and String values
Map<String, String> countries = new HashMap<>();
// Adding entries to the HashMap
countries.put("India", "IN");
countries.put("United States", "US");
countries.put("Canada", "CA");
// Using forEach method with a lambda expression to print each entry
countries.forEach((key, value) -> System.out.println("Key: " + key + ", Value: " + value));
}
}
Output:
Key: India, Value: IN
Key: United States, Value: US
Key: Canada, Value: CA
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;
import java.util.Map;
public class PrintEmployeeDetails {
public static void main(String[] args) {
// Creating a Map with String keys (employee IDs) and String values (employee names)
Map<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.