What is the ConcurrentHashMap.compute() method in Java?
A
ConcurrentHashMapis a thread-safe version of a HashMap that allows concurrent read and thread-safe update operations.
The compute() method uses the mapping function provided as an argument to compute a new value for the specified key.
If the specified key is not present, then no operation is performed and the method returns null.
nullis not allowed as a key or value.
The ConcurrentHashMap object, which uses HashMap internally, is divided into multiple portions according to the concurrency level. During an update operation, only a specific portion of the map is locked instead of the whole map.
Since it doesn’t lock the whole map, there may be a chance of a read operation (like
get()) overlapping with an update operation (likeput()andremove()). In such a case, the result of the read operation will reflect the most recently completed update operation.
Read more about ConcurrentHashMap here.
Syntax
public V compute(K key, BiFunction remappingFunction)
Parameters
-
key: The key for which the new value is to be computed. -
remappingFunction:- A
BiFunctionthat takes two arguments as input and returns a single value. - The value returned from the
BiFunctionwill be updated as the value of the passed key of the map. - If we return
nullfromBiFunction, then the mapping for the key will be removed. - If the function itself throws an exception, then the exception is rethrown, and the current mapping is left unchanged.
- A
Return value
The compute() method returns the new value associated with the key.
Code
The code below demonstrates how to use the compute() method.
import java.util.concurrent.ConcurrentHashMap;class Compute {public static void main( String args[] ) {ConcurrentHashMap<String, Integer> map = new ConcurrentHashMap<>();map.put("Maths", 50);map.put("Science", 60);map.put("Programming", 70);System.out.println( "The map is - " + map);System.out.println( "\nCalling compute function for key Maths");Integer newVal = map.compute("Maths", (key, oldVal) -> { return oldVal + 10; });System.out.println("\nThe return value is " + newVal);System.out.println( "The map is - " + map);System.out.println( "\n---------------\n");System.out.println( "Calling compute function for key Economics\n");newVal =map.compute("Economics",(key, oldVal) -> {System.out.print("Inside BiFunction: The key is ");System.out.print(key);System.out.print(". The value is ");System.out.println(oldVal + ".");if(oldVal != null) {return oldVal + 10;}return null;});System.out.println("\nThe return value is " + newVal);System.out.println( "The map is - " + map);}}
Explanation
-
In line 5, we create a
ConcurrentHashMapnamedmap. -
In lines 6-8, we add entries to the map.
-
In line 13, we call the
compute()method on themapfor theMathskey.
The code will compute a new value inside the
BiFunctionand update the value associated with theMathskey.
-
Line 20 calls the
compute()method on themapfor theEconomicskey.- There is no mapping associated with the
Economicskey, so the map remains unchanged and returnsnull.
- There is no mapping associated with the