The compute(Key, BiFunction) method of ConcurrentHashMap class is used to compute a mapping for the specified key and its current mapped value (or null if there is no current mapping is found).
- This method is used to atomically update a value for given key in ConcurrentHashMap.
- If the remapping function throws an exception, the exception is re-thrown, and the current mapping is left unchanged.
- During computation, update process on this map from other threads has been blocked so the computation must not attempt to update any other mappings of this Map
-
For example, This mapping append string value of mapping:
ConcurrentHashMap.compute(key, (key, value) -> (value == null) ? msg : value.concat(msg))
Syntax:
public V
compute(K key,
BiFunction<? super K, ? super V,
? extends V> remappingFunction)
Parameters: This method accepts two parameters:
- key: key with which the value is to be associated.
- remappingFunction: function to do the operation on value.
Returns: This method returns new value associated with the specified key, or null if none.
Exception: This method throws folloeiong exceptions:
- NullPointerException: if the specified key or remappingFunction is null.
- llegalStateException: if the computation try to do a recursive update to this map that would otherwise never complete.
- RuntimeException: if the remappingFunction does so, in which case the mapping is unchanged.
Below programs illustrate the compute(Key, BiFunction) method:
Program 1:
// Java program to demonstrate // compute(Key, BiFunction) method. import java.util.concurrent.*; public class GFG { // Main method public static void main(String[] args) { // create a ConcurrentHashMap and add some values ConcurrentHashMap<String, Integer> map = new ConcurrentHashMap<>(); map.put("Book1", 10); map.put("Book2", 500); map.put("Book3", 400); // print map details System.out.println("ConcurrentHashMap: " + map.toString()); // remap the values of ConcurrentHashMap // using compute method map.compute("Book2", (key, val) -> val + 100); map.compute("Book1", (key, val) -> val + 512); // print new mapping System.out.println("New ConcurrentHashMap: " + map); } } |
ConcurrentHashMap: {Book3=400, Book1=10, Book2=500}
New ConcurrentHashMap: {Book3=400, Book1=522, Book2=600}
Program 2:
// Java program to demonstrate // compute(Key, BiFunction) method. import java.util.concurrent.*; public class GFG { // Main method public static void main(String[] args) { // create a ConcurrentHashMap and add some values ConcurrentHashMap<Integer, String> map = new ConcurrentHashMap<>(); map.put(1, "Kolkata"); map.put(2, "Nadia"); map.put(3, "Howrah"); // print map details System.out.println("ConcurrentHashMap: " + map.toString()); // remap the values of ConcurrentHashMap // using compute method map.compute(2, (key, val) -> val.concat(" (West-Bengal)")); map.compute(3, (key, val) -> val.concat(" (West-Bengal)")); // print new mapping System.out.println("New ConcurrentHashMap: " + map); } } |
ConcurrentHashMap: {1=Kolkata, 2=Nadia, 3=Howrah}
New ConcurrentHashMap: {1=Kolkata, 2=Nadia (West-Bengal), 3=Howrah (West-Bengal)}
Program 3:To show NullPointerException
// Java program to demonstrate NullPointerException // for compute(Key, BiFunction) method. import java.util.concurrent.*; public class GFG { // Main method public static void main(String[] args) { // create a ConcurrentHashMap and add some values ConcurrentHashMap<Integer, String> map = new ConcurrentHashMap<>(); map.put(1, "Kolkata"); map.put(2, "Nadia"); map.put(3, "Howrah"); try { // remap the values of ConcurrentHashMap // using compute method map.compute(null, (key, val) -> val.concat(" (West-Bengal)")); } catch (NullPointerException e) { // print Exception System.out.println("Exception: " + e); } } } |
Exception: java.lang.NullPointerException
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- ConcurrentHashMap keys() method in Java with Examples
- ConcurrentHashMap elements() method in Java with Examples
- ConcurrentHashMap computeIfAbsent() method in Java with Examples
- ConcurrentHashMap entrySet() method in Java with Examples
- ConcurrentHashMap contains() method in Java with Examples
- ConcurrentHashMap values() method in Java with Examples
- ConcurrentHashMap keySet() method in Java with Examples
- ConcurrentHashMap put() method in Java
- ConcurrentHashMap remove() method in Java
- ConcurrentHashMap containsValue() Method in Java
- ConcurrentHashMap containsKey() Method in Java
- ConcurrentHashMap get() Method in Java
- ConcurrentHashMap size() Method in Java
- ConcurrentHashMap isEmpty() Method in Java
- ConcurrentHashMap putAll() method in Java
- ConcurrentHashMap putIfAbsent() Method in Java
- ConcurrentHashMap in Java
- Java ConcurrentHashMap | clear()
- Difference between HashMap and ConcurrentHashMap
- HashMap compute() method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

