Java ConcurrentHashMap computeIfAbsent() MethodThe 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. SyntaxParameterkey - key with which the specified value is to be associated mappingFunction - the function to compute a value ReturnsThe current (existing or computed) value associated with the specified key, or null if the computed value is null ThrowsNullPointerException IllegalStateException RuntimeException Example 1Test it NowOutput: 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 2Test it NowOutput: ConcurrentHashMap values : {vowels=5, digits=9, alfabets=26} new ConcurrentHashMap after computeIfAbsent : {vowels=5, consonent=21, digits=9, alfabets=26}
Next TopicJava ConcurrentHashMap
|