Home » Java ConcurrentHashMap computeIfPresent() Method

Java ConcurrentHashMap computeIfPresent() Method

by Online Tutorials Library

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

Test it Now

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

Test it Now

Output:

ConcurrentHashMap values :   {A=26, B=98, C=55}  new ConcurrentHashMap after  computeIfPresent :   {A=26, B=98, C=155}  

You may also like