Open In App

ConcurrentHashMap put() method in Java

Last Updated : 20 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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:

  1. K is the type of key in the map.
  2. V is the type of value in the map.
  3. Key is the key to be associated with the given value.
  4. value is the value to be associated with the given key.
  5. 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




// Java program to demonstrate
// put() method
 
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
 
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);
    }
}


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




// Java program to demonstrate
// put() method
 
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
 
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);
    }
}


Output

{1=1, 2=1, 3=1, 4=1, 5=1, 6=1}

Program 2: When an existing Key, Value is passed. 

Java




// Java program to demonstrate
// put() method
 
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
 
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);
    }
}


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




// Java program to demonstrate
// put() method
 
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
 
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");
        }
    }
}


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
Previous
Next
Share your thoughts in the comments

Similar Reads