Javatpoint Logo
Javatpoint Logo

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}





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA