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 following 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
import java.util.concurrent.*;
public class GFG {
public static void main(String[] args)
{
ConcurrentHashMap<String, Integer>
map = new ConcurrentHashMap<>();
map.put("Book1", 10 );
map.put("Book2", 500 );
map.put("Book3", 400 );
System.out.println("ConcurrentHashMap: "
+ map.toString());
map.compute("Book2", (key, val)
-> val + 100 );
map.compute("Book1", (key, val)
-> val + 512 );
System.out.println("New ConcurrentHashMap: "
+ map);
}
}
|
Output:
ConcurrentHashMap: {Book3=400, Book1=10, Book2=500}
New ConcurrentHashMap: {Book3=400, Book1=522, Book2=600}
Program 2:
Java
import java.util.concurrent.*;
public class GFG {
public static void main(String[] args)
{
ConcurrentHashMap<Integer, String>
map = new ConcurrentHashMap<>();
map.put( 1 , "Kolkata");
map.put( 2 , "Nadia");
map.put( 3 , "Howrah");
System.out.println("ConcurrentHashMap: "
+ map.toString());
map.compute( 2 , (key, val)
-> val.concat(" (West-Bengal)"));
map.compute( 3 , (key, val)
-> val.concat(" (West-Bengal)"));
System.out.println("New ConcurrentHashMap: "
+ map);
}
}
|
Output:
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
import java.util.concurrent.*;
public class GFG {
public static void main(String[] args)
{
ConcurrentHashMap<Integer, String>
map = new ConcurrentHashMap<>();
map.put( 1 , "Kolkata");
map.put( 2 , "Nadia");
map.put( 3 , "Howrah");
try {
map.compute( null , (key, val)
-> val.concat(" (West-Bengal)"));
}
catch (NullPointerException e) {
System.out.println("Exception: " + e);
}
}
}
|
Output:
Exception: java.lang.NullPointerException
References: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentHashMap.html#compute-K-java.util.function.BiFunction-
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!