ConcurrentHashMap remove() method in Java
The remove() method in Java’s ConcurrentHashMap class is used to remove the entry for a given key from the map. It has the following signature:
V remove(Object key)
where:
- key is the key of the entry to be removed.
- V is the type of values in the map.
- The remove() 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 remove() operation is performed, the method first acquires the lock on the segment of the map corresponding to the given key. If the key is present in the map, the entry is removed and the corresponding value is returned. If the key is not present in the map, the method simply returns null.
Here is an example of using the remove() method:
Java
import java.util.concurrent.ConcurrentHashMap; public class ConcurrentHashMapExample { public static void main(String[] args) { ConcurrentHashMap<String, Integer> map = new ConcurrentHashMap<>(); map.put( "Alice" , 25 ); map.put( "Bob" , 30 ); map.put( "Charlie" , 35 ); System.out.println( map); // {Charlie=35, Bob=30, Alice=25} Integer value = map.remove( "Bob" ); System.out.println( "Removed value for Bob: " + value); // 30 System.out.println(map); // {Charlie=35, Alice=25} value = map.remove( "Dave" ); System.out.println( "Removed value for Dave: " + value); // null System.out.println(map); // {Charlie=35, Alice=25} } } |
{Bob=30, Alice=25, Charlie=35} Removed value for Bob: 30 {Alice=25, Charlie=35} Removed value for Dave: null {Alice=25, Charlie=35}
In this example, we first create a ConcurrentHashMap and add three key-value pairs to it using the put() method. We then print out the contents of the map.
Next, we perform a remove() operation with an existing key (“Bob”) to remove the corresponding entry from the map. The method returns the value associated with the key (30), which we print out. We then print out the contents of the map again to verify that the entry for “Bob” has been removed.
Finally, we perform a remove() operation with a non-existing key (“Dave”) and print out the value returned by the method (which should be null). We then print out the contents of the map again to verify that the map remains unchanged.
A. remove(Object key)
The remove(Object key) method of the class ConcurrentHashmap in Java is used to remove the mapping from the map. If the key does not exist in the map, then this function does nothing.
Syntax:
public V remove(Object key)
Parameters:
This method accepts a mandatory parameter key which is the key that needs to be removed
Return Value:
This method returns the previous value associated with key, or null if there was no mapping for key.
Exceptions:
This method throws NullPointerException if the specified key is null.
Below are the programs to illustrate the remove() method:
Program 1:
Java
// Java program to demonstrate remove() 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( "Map: " + my_cmmap); System.out.println(); // Removing the mapping // with existing key 6 // using remove() method String valueRemoved = my_cmmap.remove( "6" ); // Printing the map after remove() System.out.println( "After removing mapping with key 6:" ); System.out.println( "Map: " + my_cmmap); System.out.println( "Value removed: " + valueRemoved); System.out.println(); // Removing the mapping // with non-existing key 10 // using remove() method valueRemoved = my_cmmap.remove( "10" ); // Printing the map after remove() System.out.println( "After removing mapping with key 10:" ); System.out.println( "Map: " + my_cmmap); System.out.println( "Value removed: " + valueRemoved); } } |
Map: {1=1, 2=1, 3=1, 4=1, 5=1, 6=1} After removing mapping with key 6: Map: {1=1, 2=1, 3=1, 4=1, 5=1} Value removed: 1 After removing mapping with key 10: Map: {1=1, 2=1, 3=1, 4=1, 5=1} Value removed: null
Program 2: To demonstrate NullPointerException with null key
Java
// Java program to demonstrate remove() 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( "Map: " + my_cmmap); System.out.println(); try { // Removing the mapping with null key // using remove() method my_cmmap.remove( null ); } catch (Exception e) { System.out.println( "Exception: " + e); } } } |
Map: {1=1, 2=1, 3=1, 4=1, 5=1, 6=1} Exception: java.lang.NullPointerException
B. remove(Object key, Object value)
The remove(Object key, Object value) method of class ConcurrentHashmap in Java is used to remove the mapping from the map. The mapping with the specified (key, value) pair is searched in the map and remove if found, and return true. If the key does not exist in the map, then this function does nothing and returns false.
Syntax:
public boolean remove(Object key, Object value)
Parameters:
This method accepts two parameters key - key with which the specified value is associated. value - value expected to be associated with the specified key.
Return Value:
This method returns true if the value was removed. Else it returns false.
Exceptions:
This method throws NullPointerException if the specified key is null.
Below are the programs to illustrate remove() method:
Program 1:
Java
// Java program to demonstrate remove() 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( "Map: " + my_cmmap); System.out.println(); // Removing the mapping // with existing pair 6, 1 // using remove() method boolean valueRemoved = my_cmmap.remove( "6" , "1" ); // Printing the map after remove() System.out.println( "After removing mapping with pair (6, 1):" ); System.out.println( "Map: " + my_cmmap); System.out.println( "Was value removed: " + valueRemoved); System.out.println(); // Removing the mapping // with non-existing pair 1, 2 // using remove() method valueRemoved = my_cmmap.remove( "1" , "2" ); // Printing the map after remove() System.out.println( "After removing mapping with pair (1, 2):" ); System.out.println( "Map: " + my_cmmap); System.out.println( "Was value removed: " + valueRemoved); } } |
Map: {1=1, 2=1, 3=1, 4=1, 5=1, 6=1} After removing mapping with pair (6, 1): Map: {1=1, 2=1, 3=1, 4=1, 5=1} Was value removed: true After removing mapping with pair (1, 2): Map: {1=1, 2=1, 3=1, 4=1, 5=1} Was value removed: false
Program 2: To demonstrate NullPointerException with null key
Java
// Java program to demonstrate // remove(key, value) null key 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( "Map: " + my_cmmap); System.out.println(); try { // Removing the mapping // with null key // using remove() method my_cmmap.remove( null , "1" ); } catch (Exception e) { System.out.println( "Exception: " + e); } } } |
Map: {1=1, 2=1, 3=1, 4=1, 5=1, 6=1} Exception: java.lang.NullPointerException
Program 3: To demonstrate NullPointerException with the null value.
Java
// Java program to demonstrate // remove(key, value) null value 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( "Map: " + my_cmmap); try { // Removing the mapping with correct key and // null value using remove() method my_cmmap.remove( "1" , null ); my_cmmap.remove( "7" , null ); } catch (Exception e) { System.out.println( "Exception: " + e); } } } |
Map: {1=1, 2=1, 3=1, 4=1, 5=1, 6=1}
Please Login to comment...