ConcurrentHashMap put() method in Java
The put() method of 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 program to demonstrate // put() method import java.util.concurrent.ConcurrentHashMap; import java.util.*; public class ConcurrentHashMapExample { public static void main(String[] args) { // Creating ConcurrentHashMap Map<String, String> my_cmmap = new ConcurrentHashMap<String, String>(); // Adding elements to the map // using put() method 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" ); // Printing the map System.out.print(my_cmmap); } } |
{1=1, 2=1, 3=1, 4=1, 5=1, 6=1}
Program 2: When an existing Key, Value is passed.
// Java program to demonstrate // put() method import java.util.concurrent.ConcurrentHashMap; import java.util.*; public class ConcurrentHashMapExample { public static void main(String[] args) { // Creating ConcurrentHashMap Map<String, String> my_map = new ConcurrentHashMap<String, String>(); // Variable to get the returned value by put() String returnedValue; // Adding elements to the map // using put() method // and printing the returned value each time 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); } } |
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 program to demonstrate // put() method import java.util.concurrent.ConcurrentHashMap; import java.util.*; public class ConcurrentHashMapExample { public static void main(String[] args) { // Creating ConcurrentHashMap Map<String, String> my_cmmap = new ConcurrentHashMap<String, String>(); // Adding elements to the map // using put() method 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" ); // Printing the map System.out.println(my_cmmap + "\n" ); // When null key is passed 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" ); } // When null value is passed 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" ); } } } |
{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