The ConcurrentHashMap.put()
method in Java is used to insert key-value pairs into a ConcurrentHashMap
.
Table of Contents
- Introduction
put
Method Syntax- Examples
- Adding Entries to a ConcurrentHashMap
- Handling Duplicate Keys
- Real-World Use Case
- Example: Managing a User Session Store
- Conclusion
Introduction
The ConcurrentHashMap.put()
method is a member of the ConcurrentHashMap
class in Java. It allows you to add or update key-value pairs in a thread-safe manner. If the key is not already present in the map, the method inserts the new key-value pair and returns null
. If the key is already present, the method updates the value associated with the key and returns the previous value.
The ConcurrentHashMap
class is part of the java.util.concurrent
package, designed for high concurrency and scalability.
put() Method Syntax
The syntax for the put
method is as follows:
public V put(K key, V value)
- The method takes two parameters:
key
of typeK
, which represents the key to be inserted or updated.value
of typeV
, which represents the value to be associated with the key.
- The method returns the previous value associated with the key, or
null
if there was no mapping for the key.
Examples
Adding Entries to a ConcurrentHashMap
The put
method can be used to add key-value pairs to a ConcurrentHashMap
.
Example
import java.util.concurrent.ConcurrentHashMap;
public class PutExample {
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);
// Printing the ConcurrentHashMap
System.out.println("ConcurrentHashMap: " + people);
}
}
Output:
ConcurrentHashMap: {Vijay=35, Priya=30, Ravi=25}
Handling Duplicate Keys
The put
method returns the previous value if the key is already present in the ConcurrentHashMap
.
Example
import java.util.concurrent.ConcurrentHashMap;
public class DuplicateExample {
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
Integer previousValue1 = people.put("Ravi", 25);
Integer previousValue2 = people.put("Priya", 30);
Integer previousValue3 = people.put("Ravi", 40); // Updating the value for the key "Ravi"
// Printing the results of adding entries
System.out.println("Previous value for Ravi: " + previousValue1);
System.out.println("Previous value for Priya: " + previousValue2);
System.out.println("Previous value for Ravi after update: " + previousValue3);
// Printing the ConcurrentHashMap
System.out.println("ConcurrentHashMap: " + people);
}
}
Output:
Previous value for Ravi: null
Previous value for Priya: null
Previous value for Ravi after update: 25
ConcurrentHashMap: {Vijay=35, Priya=30, Ravi=40}
Real-World Use Case
Example: Managing a User Session Store
A common real-world use case for ConcurrentHashMap
is managing a user session store where session data is updated concurrently.
Example
import java.util.concurrent.ConcurrentHashMap;
import java.util.Map;
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");
userSessions.put("Anita", "Session4");
// Printing the user sessions
System.out.println("User Sessions: ");
for (Map.Entry<String, String> entry : userSessions.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
}
}
Output:
User Sessions:
Vijay: Session3
Priya: Session2
Ravi: Session1
Anita: Session4
In this example, ConcurrentHashMap
is used to maintain user session data in a thread-safe manner, ensuring that concurrent updates do not cause data inconsistency.
Conclusion
The ConcurrentHashMap.put()
method in Java provides a way to add or update key-value pairs 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 both the insertion of new pairs and the updating of existing pairs, making it a versatile tool for data management in multi-threaded scenarios.
Comments
Post a Comment
Leave Comment