The IdentityHashMap.containsKey()
method in Java is used to check if the map contains a mapping for the specified key.
Table of Contents
- Introduction
containsKey
Method Syntax- Examples
- Basic Usage of
containsKey
Method - Checking for Non-Existent Keys
- Basic Usage of
- Real-World Use Case
- Example: Managing Active User Sessions
- Conclusion
Introduction
The IdentityHashMap.containsKey()
method is a member of the IdentityHashMap
class in Java. This class uses reference equality (==) instead of object equality (equals()) when comparing keys. The containsKey
method checks if the map contains a mapping for the specified key.
containsKey() Method Syntax
The syntax for the containsKey
method is as follows:
public boolean containsKey(Object key)
- Parameters:
key
: The key whose presence in this map is to be tested.
- Returns:
true
if this map contains a mapping for the specified key.
Examples
Basic Usage of containsKey
Method
The containsKey
method can be used to check if a specified key is present in an IdentityHashMap
.
Example
import java.util.IdentityHashMap;
public class IdentityHashMapContainsKeyExample {
public static void main(String[] args) {
// Creating an IdentityHashMap
IdentityHashMap<String, Integer> map = new IdentityHashMap<>();
// Adding key-value pairs to the IdentityHashMap
map.put("Ravi", 25);
map.put("Priya", 30);
map.put("Vijay", 35);
// Checking if specific keys are present in the IdentityHashMap
boolean containsRavi = map.containsKey("Ravi");
boolean containsAnita = map.containsKey("Anita");
// Printing the results
System.out.println("Contains key 'Ravi': " + containsRavi);
System.out.println("Contains key 'Anita': " + containsAnita);
}
}
Output:
Contains key 'Ravi': true
Contains key 'Anita': false
Checking for Non-Existent Keys
If the specified key is not found in the IdentityHashMap
, the containsKey
method returns false
.
Example
import java.util.IdentityHashMap;
public class IdentityHashMapContainsNonExistentKeyExample {
public static void main(String[] args) {
// Creating an IdentityHashMap
IdentityHashMap<String, Integer> map = new IdentityHashMap<>();
// Adding key-value pairs to the IdentityHashMap
map.put("Ravi", 25);
map.put("Priya", 30);
// Checking if a non-existent key is present in the IdentityHashMap
boolean containsVijay = map.containsKey("Vijay");
// Printing the result
System.out.println("Contains key 'Vijay': " + containsVijay);
}
}
Output:
Contains key 'Vijay': false
Real-World Use Case
Example: Managing Active User Sessions
A common real-world use case for IdentityHashMap.containsKey()
is managing active user sessions in a web application where reference equality is required.
Example
import java.util.IdentityHashMap;
public class ActiveUserSessionManager {
static class UserSession {
private String sessionId;
private String userName;
public UserSession(String sessionId, String userName) {
this.sessionId = sessionId;
this.userName = userName;
}
@Override
public String toString() {
return "UserSession{" +
"sessionId='" + sessionId + '\'' +
", userName='" + userName + '\'' +
'}';
}
}
public static void main(String[] args) {
// Creating user sessions
UserSession session1 = new UserSession("S1", "Ravi");
UserSession session2 = new UserSession("S2", "Priya");
// Creating an IdentityHashMap to manage user sessions
IdentityHashMap<UserSession, String> sessionMap = new IdentityHashMap<>();
sessionMap.put(session1, "Active");
sessionMap.put(session2, "Inactive");
// Checking if specific user sessions are active
boolean containsSession1 = sessionMap.containsKey(session1);
UserSession session3 = new UserSession("S3", "Vijay");
boolean containsSession3 = sessionMap.containsKey(session3);
// Printing the results
System.out.println("Contains session 1: " + containsSession1);
System.out.println("Contains session 3: " + containsSession3);
}
}
Output:
Contains session 1: true
Contains session 3: false
In this example, IdentityHashMap.containsKey()
is used to check if specific user sessions are active, where sessions are identified by reference equality, making it suitable for scenarios where unique object references are crucial.
Conclusion
The IdentityHashMap.containsKey()
method in Java provides a way to check if the map contains a mapping for the specified key, using reference equality for key comparison. By understanding how to use this method, you can efficiently manage collections of key-value pairs where reference equality is required. This method allows you to utilize the power of IdentityHashMap
for various scenarios, making it a versatile tool for managing collections of key-value pairs based on reference equality.
Comments
Post a Comment
Leave Comment