🎓 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.keySet() method in Java is used to obtain a set view of the keys contained in the ConcurrentHashMap.
Table of Contents
- Introduction
keySetMethod Syntax- Examples
- Retrieving Keys from a ConcurrentHashMap
- Iterating Over Keys
- Real-World Use Case
- Example: Listing Active User Sessions
- Conclusion
Introduction
The ConcurrentHashMap.keySet() method is a member of the ConcurrentHashMap class in Java. It provides a set view of the keys contained in the map. The ConcurrentHashMap class is part of the java.util.concurrent package, designed for high concurrency and scalability.
keySet() Method Syntax
The syntax for the keySet method is as follows:
public Set<K> keySet()
- The method takes no parameters.
- The method returns a
Setview of the keys contained in the map.
Examples
Retrieving Keys from a ConcurrentHashMap
The keySet method can be used to retrieve the keys from a ConcurrentHashMap.
Example
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
public class KeySetExample {
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);
// Retrieving the set of keys
Set<String> keys = people.keySet();
// Printing the set of keys
System.out.println("Keys in ConcurrentHashMap: " + keys);
}
}
Output:
Keys in ConcurrentHashMap: [Ravi, Priya, Vijay]
Iterating Over Keys
You can iterate over the keys obtained from the keySet method.
Example
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
public class IterateKeysExample {
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);
// Retrieving the set of keys
Set<String> keys = people.keySet();
// Iterating over the keys
System.out.println("Iterating over keys:");
for (String key : keys) {
System.out.println(key);
}
}
}
Output:
Iterating over keys:
Ravi
Priya
Vijay
Real-World Use Case
Example: Listing Active User Sessions
A common real-world use case for ConcurrentHashMap is managing user session data and listing active sessions.
Example
import java.util.Set;
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");
// Retrieving the set of user session keys
Set<String> sessionKeys = userSessions.keySet();
// Printing the active user sessions
System.out.println("Active User Sessions:");
for (String key : sessionKeys) {
if ("Active".equals(userSessions.get(key))) {
System.out.println(key);
}
}
}
}
Output:
Active User Sessions:
Ravi
Vijay
In this example, ConcurrentHashMap is used to manage user session data, and the keySet method is employed to list active user sessions in a thread-safe manner.
Conclusion
The ConcurrentHashMap.keySet() method in Java provides a way to obtain a set view of the keys contained 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 retrieve and iterate over keys, 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