ConcurrentHashMap put() method in Java
Last Updated :
20 Feb, 2023
The put() method in ConcurrentHashMap class in Java is used to associate a given value with a given key in the map. It has the following signature:
V put(K key, V value)
where:
- K is the type of key in the map.
- V is the type of value in the map.
- Key is the key to be associated with the given value.
- value is the value to be associated with the given key.
- put() method works in a concurrent environment, which means that multiple threads can access and modify the concurrentHashMap instance at the same time, without any data race or synchronization issues.
When a put() operation is performed, the method first acquires the lock on the segment of the map corresponding to the given key. If the key is already present in the map, the old value associated with the key is replaced by the new value. If the key is not present in the map, a new entry is added with the given key-value pair.
The put() method returns the previous value associated with the given key, or null if there was no previous mapping for the key. If you don’t need the previous value, you can simply ignore the return value.
Java
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
public class ConcurrentHashMapExample {
public static void main(String[] args)
{
Map<String, String> my_cmmap
= new ConcurrentHashMap<String, String>();
my_cmmap.put( "1" , "1" );
my_cmmap.put( "2" , "1" );
my_cmmap.put( "3" , "1" );
my_cmmap.put( "4" , "1" );
my_cmmap.put( "5" , "1" );
my_cmmap.put( "6" , "1" );
System.out.print(my_cmmap);
}
}
|
Output
{Bob=30, Alice=25, Charlie=35}
Old value for Bob: 30
{Bob=40, Alice=25, Charlie=35}
New value for Dave: null
{Bob=40, Alice=25, Charlie=35, Dave=45}
The put() method of the class ConcurrentHashmap in Java is used to insert a mapping into this map. It takes the parameter as a (Key, Value) pair. If an existing key is passed with a value, then this method updates the value of that key. Else if a new pair is passed, then the pair gets inserted as a whole.
Syntax:
public V put(K key, V value)
Parameters: This method accepts two mandatory parameters:
- key: – key with which the specified value is to be associated
- value: – value to be associated with the specified key
Return Value: This method returns the previous value associated with key, or null if there was no mapping for key.
Exception: This method throws NullPointerException if the specified key or value is null Below is the example to illustrate put() method:
Program 1: When the Key, Value passed is new.
Java
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
public class ConcurrentHashMapExample {
public static void main(String[] args)
{
Map<String, String> my_cmmap
= new ConcurrentHashMap<String, String>();
my_cmmap.put( "1" , "1" );
my_cmmap.put( "2" , "1" );
my_cmmap.put( "3" , "1" );
my_cmmap.put( "4" , "1" );
my_cmmap.put( "5" , "1" );
my_cmmap.put( "6" , "1" );
System.out.print(my_cmmap);
}
}
|
Output
{1=1, 2=1, 3=1, 4=1, 5=1, 6=1}
Program 2: When an existing Key, Value is passed.
Java
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
public class ConcurrentHashMapExample {
public static void main(String[] args)
{
Map<String, String> my_map
= new ConcurrentHashMap<String, String>();
String returnedValue;
returnedValue = my_map.put( "Geek" , "100" );
System.out.println( "Map: " + my_map);
System.out.println( "Returned Value: "
+ returnedValue);
System.out.println();
returnedValue = my_map.put( "Geek" , "200" );
System.out.println( "Map: " + my_map);
System.out.println( "Returned Value: "
+ returnedValue);
System.out.println();
returnedValue = my_map.put( "Geek" , "300" );
System.out.println( "Map: " + my_map);
System.out.println( "Returned Value: "
+ returnedValue);
System.out.println();
returnedValue = my_map.put( "Geek" , "400" );
System.out.println( "Map: " + my_map);
System.out.println( "Returned Value: "
+ returnedValue);
System.out.println();
returnedValue = my_map.put( "Geek" , "500" );
System.out.println( "Map: " + my_map);
System.out.println( "Returned Value: "
+ returnedValue);
System.out.println();
System.out.print(my_map);
}
}
|
Output
Map: {Geek=100}
Returned Value: null
Map: {Geek=200}
Returned Value: 100
Map: {Geek=300}
Returned Value: 200
Map: {Geek=400}
Returned Value: 300
Map: {Geek=500}
Returned Value: 400
{Geek=500}
Program 3: To demonstrate NullPointerException
Java
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
public class ConcurrentHashMapExample {
public static void main(String[] args)
{
Map<String, String> my_cmmap
= new ConcurrentHashMap<String, String>();
my_cmmap.put( "1" , "1" );
my_cmmap.put( "2" , "1" );
my_cmmap.put( "3" , "1" );
my_cmmap.put( "4" , "1" );
my_cmmap.put( "5" , "1" );
my_cmmap.put( "6" , "1" );
System.out.println(my_cmmap + "\n" );
try {
System.out.println( "When (null, \"10\") "
+ "is passed as parameter:" );
my_cmmap.put( null , "10" );
}
catch (Exception e) {
System.out.println( "Exception: " + e + "\n" );
}
try {
System.out.println( "When (\"10\", null) "
+ "is passed as parameter:" );
my_cmmap.put( "10" , null );
}
catch (Exception e) {
System.out.println( "Exception: " + e + "\n" );
}
}
}
|
Output
{1=1, 2=1, 3=1, 4=1, 5=1, 6=1}
When (null, "10") is passed as parameter:
Exception: java.lang.NullPointerException
When ("10", null) is passed as parameter:
Exception: java.lang.NullPointerException
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...