82
Java ConcurrentHashMap compute() Method
The compute() method of ConcurrentHashMap class is used to compute the mapping for the specified key and its current value.
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:
Values in map with detail: {1=1, 2=10, 3=100, 4=2, 5=20, 6=200} Map after using compute(): {1=1, 2=10, 3=100, 4=2, 5=20, 6=200100}
Example 2
Output:
ConcurrentHashMap value detail: {1=Java, 2=php, 3=.net} New ConcurrentHashMap after compute: {1=Java, 2=php (Angular), 3=.net (java Script)}
Next TopicJava ConcurrentHashMap