Java ConcurrentHashMap: computeIfAbsent() Example
February 12, 2022
On this page we will learn computeIfAbsent()
method of Java ConcurrentHashMap
class.
Find the Java doc of
computeIfAbsent
method.
V computeIfAbsent(K key, Function<? super K,? extends V> mappingFunction)
2. If the specified key is already present, the mappingFunction is not computed and there is no change in the map.
3. If the specified key is absent and the mappingFunction computes a value as null then this new key/value is not added to map.
4. The entire method invocation is performed atomically.
5. The mapping function must not modify this map during computation.
6. Parameters :
key - key to associate a value.
mappingFunction - the function to compute a value.
7. Returns : New value after computation or null.
8. Throws :
NullPointerException: If specified key or provided mappingFunction is null.
IllegalStateException: If computation is recursive.
RuntimeException: If mappingFunction throws
RuntimeException
.
Example-1
ComputeIfAbsent1.javapackage com.concretepage; import java.util.concurrent.ConcurrentHashMap; public class ComputeIfAbsent1 { public static void main(String[] args) { ConcurrentHashMap<Integer, String> conMap = new ConcurrentHashMap<>(); conMap.put(10, "Varanasi"); conMap.put(20, "Prayag"); conMap.put(30, "Ayodhya"); System.out.println("--- key is present ---"); String v1 = conMap.computeIfAbsent(20, k -> k + " - Kanyakumari"); System.out.println("v1: " + v1); System.out.println(conMap); // No change System.out.println("--- key is not present but value is null ---"); String v2 = conMap.computeIfAbsent(40, k -> null); System.out.println("v2: " + v2); System.out.println(conMap); // No change System.out.println("--- key is not present ---"); String v3 = conMap.computeIfAbsent(50, k -> k + " - Kanyakumari"); System.out.println("v3: " + v3); System.out.println(conMap); // New entry added } }
--- key is present --- v1: Prayag {20=Prayag, 10=Varanasi, 30=Ayodhya} --- key is not present but value is null --- v2: null {20=Prayag, 10=Varanasi, 30=Ayodhya} --- key is not present --- v3: 50 - Kanyakumari {50=50 - Kanyakumari, 20=Prayag, 10=Varanasi, 30=Ayodhya}
Example-2
ComputeIfAbsent2.javapackage com.concretepage; import java.util.concurrent.ConcurrentHashMap; public class ComputeIfAbsent2 { public static void main(String[] args) { ConcurrentHashMap<Integer, Integer> conMap = new ConcurrentHashMap<>(); conMap.put(10, 100); conMap.put(20, 200); conMap.put(30, 300); System.out.println("--- key is present ---"); Integer v1 = conMap.computeIfAbsent(20, k -> k * 10); System.out.println("v1: " + v1); System.out.println(conMap); // No change System.out.println("--- key is not present but value is null ---"); Integer v2 = conMap.computeIfAbsent(40, k -> null); System.out.println("v2: " + v2); System.out.println(conMap); // No change System.out.println("--- key is not present ---"); Integer v3 = conMap.computeIfAbsent(50, k -> k * 10); System.out.println("v3: " + v3); System.out.println(conMap); // New entry added } }
--- key is present --- v1: 200 {20=200, 10=100, 30=300} --- key is not present but value is null --- v2: null {20=200, 10=100, 30=300} --- key is not present --- v3: 500 {50=500, 20=200, 10=100, 30=300}