Java ConcurrentHashMap: computeIfPresent() Example

By Arvind Rai, February 11, 2022
On this page we will learn computeIfPresent() method of Java ConcurrentHashMap class.
Find the Java doc of computeIfPresent method.
V computeIfPresent(K key, BiFunction<? super K,? super V,? extends V> remappingFunction) 
1. The computeIfPresent method performs computation only if the value for the specified key is present.
2. The computeIfPresent method attempts to compute a new mapping for given key and its current mapped value.
3. The entire method invocation is performed atomically.
4. The remappingFunction executes only if there is corresponding value for the key.
5. The remapping function must not modify this map during computation.

6. Parameters :
key - key of the map.
remappingFunction - the function to compute a value.

7. Returns : New value after computation or null.

8. Throws :
NullPointerException: If specified key or provided remappingFunction is null.
IllegalStateException: If computation is recursive.
RuntimeException: If remappingFunction throws RuntimeException.

Example-1

ComputeIfPresent1.java
package com.concretepage;
import java.util.concurrent.ConcurrentHashMap;

public class ComputeIfPresent1 {
  public static void main(String[] args) {
	ConcurrentHashMap<Integer, String> conMap = new ConcurrentHashMap<>();
	conMap.put(10, "Varanasi");
	conMap.put(20, "Prayag");
	conMap.put(30, "Prayag");	

	System.out.println("--- 1 ---");
	String v1 = conMap.computeIfPresent(10, (k, v) -> v + "-" + k * 2);
	System.out.println("v1: " + v1);
	System.out.println(conMap);
	
	System.out.println("--- 2 ---");
	String v2 = conMap.computeIfPresent(30, (k, v) -> v + "-" + k * 2);
	System.out.println("v2: " + v2);
	System.out.println(conMap);	
  }
} 
Output
--- 1 ---
v1: Varanasi-20
{20=Prayag, 10=Varanasi-20, 30=Prayag}
--- 2 ---
v2: Prayag-60
{20=Prayag, 10=Varanasi-20, 30=Prayag-60} 

Example-2

ComputeIfPresent2.java
package com.concretepage;
import java.util.concurrent.ConcurrentHashMap;

public class ComputeIfPresent2 {
  public static void main(String[] args) {
	ConcurrentHashMap<String, Integer> conMap = new ConcurrentHashMap<>();
	conMap.put("Mahesh", 22);
	conMap.put("Nilesh", 25);
	conMap.put("Yogesh", 20);

	System.out.println("--- 1 ---");
	Integer v1 = conMap.computeIfPresent("Nilesh", (k, v) -> k.length() + v);
	System.out.println("v1: " + v1);
	System.out.println(conMap);

	System.out.println("--- 2 ---");
	Integer v2 = conMap.computeIfPresent("Yogesh", (k, v) -> k.length() + v);
	System.out.println("v2: " + v2);
	System.out.println(conMap);	
  }
} 

Example-3

computeIfPresent vs compute()
In this example we are showing the difference between computeIfPresent and compute() methods. In computeIfPresent method, the remappingFunction is only executed if specified key is present in map. In compute() method, remappingFunction is executed even if specified key is not present.
In this example, we are specifying a key which is not present in map. As key is not present, so value corresponding to key is null. Our remappingFunction, in our example, is in such a way that if value is null, it will throw exception. In the output, we will see that computeIfPresent method will not throw error because remappingFunction is not executed. But the compute() method throws error because remappingFunction is executed.
ComputeIfPresent3.java
package com.concretepage;
import java.util.concurrent.ConcurrentHashMap;

public class ComputeIfPresent3 {
  public static void main(String[] args) {
	ConcurrentHashMap<String, Integer> conMap = new ConcurrentHashMap<>();
	conMap.put("Mahesh", 22);
	conMap.put("Nilesh", 25);

	System.out.println("--- 1 ---");
	Integer v1 = conMap.computeIfPresent("Yogesh", (k, v) -> k.length() + v);
	System.out.println("v1: " + v1);
	System.out.println(conMap);

	System.out.println("--- 2 ---");
	Integer v2 = conMap.compute("Yogesh", (k, v) -> k.length() + v);
	System.out.println("v2: " + v2);
	System.out.println(conMap);	
  }
} 
Output
--- 1 ---
v1: null
{Mahesh=22, Nilesh=25}
--- 2 ---
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.lang.Integer.intValue()" because "v" is null 

Reference

Java ConcurrentHashMap
POSTED BY
ARVIND RAI
ARVIND RAI
LEARN MORE








©2024 concretepage.com | Privacy Policy | Contact Us