Java ConcurrentHashMap putIfAbsent() MethodThe putIfAbsent() method of ConcurrentHashMap class maps the specified keys and values if the specified key is not already mapped with any value. SyntaxParameterkey - key with which the specified value is to be associated value - value to be associated with the specified key ReturnsThe previous value associated with key, or null if there was no mapping for key ThrowNullPointerException. Example 1Test it NowOutput: ConcurrentHashMap values : {vowels=5, digits=9, alfabets=26} new ConcurrentHashMap after putIfAbsent : {vowels=5, consonent=21, digits=9, alfabets=26} Example 2Test it NowOutput: 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
|