![]() |
VOOZH | about |
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).
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:
Returns: This method returns new value associated with the specified key, or null if none. Exception: This method throws following exceptions:
Below programs illustrate the compute(Key, BiFunction) method: Program 1:
ConcurrentHashMap: {Book3=400, Book1=10, Book2=500}
New ConcurrentHashMap: {Book3=400, Book1=522, Book2=600}Program 2:
ConcurrentHashMap: {1=Kolkata, 2=Nadia, 3=Howrah}
New ConcurrentHashMap: {1=Kolkata, 2=Nadia (West-Bengal), 3=Howrah (West-Bengal)}Program 3:To show NullPointerException
Exception: java.lang.NullPointerException