81
Java ConcurrentHashMap computeIfPresent() Method
The computeIfPresent() method of ConcurrentHashMap class computes a new mapping given the key and its current mapped value If the value for the specified key is present.
Syntax
Parameter
key – key with which the specified value is to be associated
remappingFunction – the function to compute a value.
Returns
the new value associated with the specified key, or null if none
Throws
NullPointerException
IllegalStateException
RuntimeException
Example 1
Output:
HashMap values : {k1=100, k2=200, k3=300, k4=400} New HashMap after computeIfPresent : {k1=100, k2=200, k3=300, k4=500}
Example 2
Output:
ConcurrentHashMap values : {A=26, B=98, C=55} new ConcurrentHashMap after computeIfPresent : {A=26, B=98, C=155}
Next TopicJava ConcurrentHashMap