106
Java ConcurrentHashMap putIfAbsent() Method
The putIfAbsent() method of ConcurrentHashMap class maps the specified keys and values if the specified key is not already mapped with any value.
Syntax
Parameter
key – key with which the specified value is to be associated
value – value to be associated with the specified key
Returns
The previous value associated with key, or null if there was no mapping for key
Throw
NullPointerException.
Example 1
Output:
ConcurrentHashMap values : {vowels=5, digits=9, alfabets=26} new ConcurrentHashMap after putIfAbsent : {vowels=5, consonent=21, digits=9, alfabets=26}
Example 2
Output:
HashMap values : {k1=100, k2=200, k3=300, k4=400} New HashMap after putIfAbsent : {k1=100, k2=200, k3=300, k4=400, k5=300, k6=10}
Next TopicJava ConcurrentHashMap