🎓 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.containsKey() method in Java is used to check if a specified key is present in a ConcurrentHashMap.
Table of Contents
- Introduction
containsKeyMethod Syntax- Examples
- Checking for Keys in a ConcurrentHashMap
- Handling Non-Existent Keys
- Real-World Use Case
- Example: Verifying User Sessions
- Conclusion
Introduction
The ConcurrentHashMap.containsKey() method is a member of the ConcurrentHashMap class in Java. It allows you to check if a specific key is present in the map.
The ConcurrentHashMap class is part of the java.util.concurrent package, designed for high concurrency and scalability.
containsKey() Method Syntax
The syntax for the containsKey method is as follows:
public boolean containsKey(Object key)
- The method takes one parameter:
keyof typeObject, which represents the key to be checked for presence in the map.
- The method returns
trueif the key is present in the map, andfalseotherwise.
Examples
Checking for Keys in a ConcurrentHashMap
The containsKey method can be used to check for the presence of keys in a ConcurrentHashMap.
Example
import java.util.concurrent.ConcurrentHashMap;
public class ContainsKeyExample {
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);
// Checking for the presence of keys
boolean hasRavi = people.containsKey("Ravi");
boolean hasAnita = people.containsKey("Anita");
// Printing the results
System.out.println("Contains key 'Ravi': " + hasRavi);
System.out.println("Contains key 'Anita': " + hasAnita);
}
}
Output:
Contains key 'Ravi': true
Contains key 'Anita': false
Handling Non-Existent Keys
The containsKey method returns false if the specified key is not present in the ConcurrentHashMap.
Example
import java.util.concurrent.ConcurrentHashMap;
public class NonExistentKeyExample {
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);
// Checking for the presence of a non-existent key
boolean hasVijay = people.containsKey("Vijay");
// Printing the result
System.out.println("Contains key 'Vijay': " + hasVijay);
}
}
Output:
Contains key 'Vijay': false
Real-World Use Case
Example: Verifying User Sessions
A common real-world use case for ConcurrentHashMap is managing user session data and verifying if a user's session exists.
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", "Session1");
userSessions.put("Priya", "Session2");
userSessions.put("Vijay", "Session3");
// Checking for the presence of user sessions
boolean sessionExists = userSessions.containsKey("Ravi");
boolean sessionNonExists = userSessions.containsKey("Anita");
// Printing the results
System.out.println("Session exists for Ravi: " + sessionExists);
System.out.println("Session exists for Anita: " + sessionNonExists);
}
Output:
Session exists for Ravi: true
Session exists for Anita: false
In this example, ConcurrentHashMap is used to manage user session data, and the containsKey method is employed to verify the existence of specific user sessions in a thread-safe manner.
Conclusion
The ConcurrentHashMap.containsKey() method in Java provides a way to check for the presence of specific keys 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 handle the presence and absence of keys, making it a versatile tool for data management in multi-threaded scenarios.
Comments
Post a Comment
Leave Comment