The computeIfAbsent(Key, Function) method of ConcurrentHashMap class which attempts to compute its value using the given mapping function for specified key if key is not already associated with a value (or is mapped to null) and enter that computed value in map else null.
- If mapping function of this method returns null, then no value is recorded for new key in map.
- This method is used to atomically update a value for given key in ConcurrentHashMap.
- If the remapping function throws an exception, the exception is rethrown, and the no mapping is recorded.
- During computation, modification this map using this method is not allowed because other threads can also uses this map.
Syntax:
public V
computeIfAbsent(K key,
Function<? super K, ? 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 current (existing or computed) value associated with the specified key, or null if mapping returns null.
Exception: This method throws:
- 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 computeIfAbsent(Key, Function) 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( "Pencil" , 1000 );
map.put( "Laptop" , 55000 );
map.put( "Clothes" , 4400 );
map.put( "Mobile" , 5300 );
System.out.println( "ConcurrentHashMap :\n "
+ map.toString());
map.computeIfAbsent( "PC" , k -> 60000 );
map.computeIfAbsent( "Charger" , k -> 800 );
System.out.println( "new ConcurrentHashMap :\n "
+ map);
}
}
|
Output: ConcurrentHashMap :
{Laptop=55000, Clothes=4400, Pencil=1000, Mobile=5300}
new ConcurrentHashMap :
{Laptop=55000, PC=60000, Clothes=4400, Pencil=1000, Charger=800, Mobile=5300}
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 , "1000RS" );
map.put( 2 , "5009RS" );
map.put( 3 , "1300RS" );
System.out.println( "ConcurrentHashMap : \n"
+ map.toString());
map.computeIfAbsent( 4 , k -> "6000RS" );
map.computeIfAbsent( 1 , k -> "8000RS" );
System.out.println( "new ConcurrentHashMap :\n"
+ map);
}
}
|
Output: ConcurrentHashMap :
{1=1000RS, 2=5009RS, 3=1300RS}
new ConcurrentHashMap :
{1=1000RS, 2=5009RS, 3=1300RS, 4=6000RS}
Program 3: To show NullPointerException
Java
import java.util.concurrent.*;
public class GFG {
public static void main(String[] args)
{
ConcurrentHashMap<String, Integer> map
= new ConcurrentHashMap<>();
map.put( "Pencil" , 1000 );
map.put( "Laptop" , 55000 );
map.put( "Clothes" , 4400 );
map.put( "Mobile" , 5300 );
try {
map.computeIfAbsent( null , k -> 60000 );
}
catch (Exception 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#computeIfAbsent-K-java.util.function.Function-