🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
The ConcurrentHashMap.forEach() method in Java is used to perform a given action for each entry in the ConcurrentHashMap.
Table of Contents
- Introduction
forEachMethod Syntax- Examples
- Basic Usage
- Using BiConsumer
- Real-World Use Case
- Example: Printing Active User Sessions
- Conclusion
Introduction
The ConcurrentHashMap.forEach() method is a member of the ConcurrentHashMap class in Java. It allows you to perform a specified action on each entry in the map. The ConcurrentHashMap class is part of the java.util.concurrent package, designed for high concurrency and scalability.
forEach() Method Syntax
The syntax for the forEach method is as follows:
public void forEach(BiConsumer<? super K, ? super V> action)
- The method takes one parameter:
actionof typeBiConsumer<? super K, ? super V>, which represents the action to be performed for each entry.
Examples
Basic Usage
The forEach method can be used to perform a specified action for each entry in a ConcurrentHashMap.
Example
import java.util.concurrent.ConcurrentHashMap;
public class ForEachExample {
public static void main(String[] args) {
// Creating a ConcurrentHashMap with String keys and Integer values
ConcurrentHashMap<String, Integer> people = new ConcurrentHashMap<>();
// Adding entries to the ConcurrentHashMap
people.put("Ravi", 25);
people.put("Priya", 30);
people.put("Vijay", 35);
// Using forEach to print each key-value pair
people.forEach((key, value) -> System.out.println(key + ": " + value));
}
}
Output:
Ravi: 25
Priya: 30
Vijay: 35
Using BiConsumer
You can use a BiConsumer to perform more complex actions on each entry.
Example
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.BiConsumer;
public class BiConsumerExample {
public static void main(String[] args) {
// Creating a ConcurrentHashMap with String keys and Integer values
ConcurrentHashMap<String, Integer> people = new ConcurrentHashMap<>();
// Adding entries to the ConcurrentHashMap
people.put("Ravi", 25);
people.put("Priya", 30);
people.put("Vijay", 35);
// Defining a BiConsumer to print a formatted message
BiConsumer<String, Integer> action = (key, value) -> System.out.println(key + " is " + value + " years old.");
// Using forEach with the BiConsumer
people.forEach(action);
}
}
Output:
Ravi is 25 years old.
Priya is 30 years old.
Vijay is 35 years old.
Real-World Use Case
Example: Printing Active User Sessions
A common real-world use case for ConcurrentHashMap is managing user session data and performing actions on each session entry.
Example
import java.util.concurrent.ConcurrentHashMap;
public class UserSessionStore {
public static void main(String[] args) {
// Creating a ConcurrentHashMap to manage user sessions
ConcurrentHashMap<String, String> userSessions = new ConcurrentHashMap<>();
// Adding user sessions to the ConcurrentHashMap
userSessions.put("Ravi", "Active");
userSessions.put("Priya", "Inactive");
userSessions.put("Vijay", "Active");
// Using forEach to print active user sessions
System.out.println("Active User Sessions:");
userSessions.forEach((key, value) -> {
if ("Active".equals(value)) {
System.out.println(key + ": " + value);
}
});
}
}
Output:
Active User Sessions:
Ravi: Active
Vijay: Active
In this example, ConcurrentHashMap is used to manage user session data, and the forEach method is employed to print active user sessions in a thread-safe manner.
Conclusion
The ConcurrentHashMap.forEach() method in Java provides a way to perform a specified action on each entry in a ConcurrentHashMap in a thread-safe manner. By understanding how to use this method, you can efficiently manage collections of key-value pairs in your Java applications, especially in concurrent environments. The method allows you to perform complex actions on each entry, making it a versatile tool for data management in multi-threaded scenarios.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment