🎓 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.
Comments
Post a Comment
Leave Comment