90
Java ConcurrentHashMap replace() Method
The replace() method of ConcurrentHashMap class replaces the entry for a key only if it is currently mapped to some value.
Syntax
Parameter
key – key with which the specified value is associated
oldValue – value expected to be associated with the specified key
newValue – value to be associated with the specified key
Returns
true if the value was replaced
Throw
NullPointerException.
Example 1
Output:
HashMap values : {k1=100, k2=200, k3=300, k4=400} New HashMap after remove : {k1=1000, k2=200, k3=300, k4=400}
Example 2
Output:
HashMap values : {k1=100, k2=200, k3=300, k4=400} New HashMap after remove : {k1=100, k2=2000, k3=300, k4=400}
Next TopicJava ConcurrentHashMap