![]() |
VOOZH | about |
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:
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.
{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:
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: nullProgram 2: To demonstrate NullPointerException with null key
Map: {1=1, 2=1, 3=1, 4=1, 5=1, 6=1}
Exception: java.lang.NullPointerExceptionB. 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:
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: falseProgram 2: To demonstrate NullPointerException with null key
Map: {1=1, 2=1, 3=1, 4=1, 5=1, 6=1}
Exception: java.lang.NullPointerExceptionProgram 3: To demonstrate NullPointerException with the null value.
Map: {1=1, 2=1, 3=1, 4=1, 5=1, 6=1}