![]() |
VOOZH | about |
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.
Syntax:
public V computeIfAbsent(K key, Function<? super K, ? extends V> remappingFunction)
Parameters: This method accepts two parameters:
Returns: This method returns current (existing or computed) value associated with the specified key, or null if mapping returns null.
Exception: This method throws:
Below programs illustrate the computeIfAbsent(Key, Function) method:
Program 1:
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:
ConcurrentHashMap :
{1=1000RS, 2=5009RS, 3=1300RS}
new ConcurrentHashMap :
{1=1000RS, 2=5009RS, 3=1300RS, 4=6000RS}Program 3: To show NullPointerException
Exception: java.lang.NullPointerException