Home » Java ConcurrentHashMap computeIfAbsent() Method

Java ConcurrentHashMap computeIfAbsent() Method

by Online Tutorials Library

Java ConcurrentHashMap computeIfAbsent() Method

The computeIfAbsent() method of ConcurrentHashMap class computes its value using the given mapping function and enters it into this map unless null If the specified key is not already associated with a value.

Syntax

Parameter

key – key with which the specified value is to be associated

mappingFunction – the function to compute a value

Returns

The current (existing or computed) value associated with the specified key, or null if the computed value is null

Throws

NullPointerException

IllegalStateException

RuntimeException

Example 1

Test it Now

Output:

HashMap values :   {k1=100, k2=200, k3=300, k4=400}  New HashMap after computeIfAbsent :   {k1=100, k2=200, k3=300, k4=400, k5=500, k6=600}  

Example 2

Test it Now

Output:

ConcurrentHashMap values :   {vowels=5, digits=9, alfabets=26}  new ConcurrentHashMap after  computeIfAbsent :   {vowels=5, consonent=21, digits=9, alfabets=26}  

You may also like