Java HashMap.computeIfPresent()

In this tutorial, we will learn about the Java HashMap.computeIfPresent() function, and learn how to use this function to compute a value for given key only if this key is present in the HashMap, with the help of examples.

computeIfPresent()

HashMap.computeIfPresent() computes the value for given key, using the given mapping function and puts this mapping into this HashMap. computeIfPresent() does this only if the specified key is already present in the HashMap.

If the key is already not present, computeIfPresent() does nothing.

The syntax of computeIfPresent() function is

computeIfPresent(K key, BiFunction<? super K,? super V,? extends V> remappingFunction)

where

ParameterDescription
keyThe key with which the specified value is to be associated.
remappingFunctionThe mapping function to compute a value for the specified key.

Returns

The function returns the computed value for the key.

Examples

1. computeIfPresent() – Key is Present

In this example, we will initialize a HashMap with mappings from String to Integer. Using computeIfPresent() function, we will compute a value for key "C". Since the key "C" is already present in the HashMap, the value for the key "C" will be updated in HashMap.

Java Program

</>
Copy
import java.util.HashMap;

public class Example{ 
	public static void main(String[] args) {
		HashMap<String,Integer> hashMap = new HashMap<>();
		hashMap.put("A",1);
		hashMap.put("B",2);
		hashMap.put("C",3);
		hashMap.put("D",4);
		System.out.println("Before compute : " + hashMap);
		
		hashMap.computeIfPresent("C",(k,v) -> v = v*v);
		System.out.println("After  compute : " + hashMap);
	}
}

Output

Before compute : {A=1, B=2, C=3, D=4}
After  compute : {A=1, B=2, C=9, D=4}

2. computeIfPresent() – Key is Not Present

In this example, we will initialize a HashMap with mappings from String to Integer. Using computeIfPresent() function, we will compute a value for key "M". Since the key "M" is not already present in the HashMap, computeIfPresent() does nothing, and the HashMap is unaffected.

Java Program

</>
Copy
import java.util.HashMap;

public class Example{ 
	public static void main(String[] args) {
		HashMap<String,Integer> hashMap = new HashMap<>();
		hashMap.put("A",1);
		hashMap.put("B",2);
		hashMap.put("C",3);
		hashMap.put("D",4);
		System.out.println("Before compute : " + hashMap);
		
		hashMap.computeIfPresent("M",(k,v) -> v = 0);
		System.out.println("After  compute : " + hashMap);
	}
}

Output

Before compute : {A=1, B=2, C=3, D=4}
After  compute : {A=1, B=2, C=3, D=4}

3. computeIfPresent() – Return Value

In this example, we will store the value returned by computeIfPresent(), and print to console.

The datatype of return value is the datatype of values of mappings in the HashMap. In this example, the datatype of the the return value is Integer.

Java Program

</>
Copy
import java.util.HashMap;

public class Example{ 
	public static void main(String[] args) {
		HashMap<String,Integer> hashMap = new HashMap<>();
		hashMap.put("A",1);
		hashMap.put("B",2);
		hashMap.put("C",3);
		hashMap.put("D",4);
		
		int value = hashMap.computeIfPresent("C",(k,v) -> v = v*v);
		System.out.println("Return Value : " + value);
	}
}

Output

Return Value : 9

4. computeIfPresent() when HashMap is null

In this example, we will initialize a HashMap with mappings from String to Integer. Using computeIfPresent() function, we will compute a value for key "C" and provide a null value for the mappingFunction. computeIfPresent() throws java.lang.NullPointerException.

Java Program

</>
Copy
import java.util.HashMap;

public class Example{ 
	public static void main(String[] args) {
		HashMap<String,Integer> hashMap = new HashMap<>();
		hashMap.put("A",1);
		hashMap.put("B",2);
		hashMap.put("C",3);
		hashMap.put("D",4);
		
		hashMap.computeIfPresent("C", null);
	}
}

Output

Exception in thread "main" java.lang.NullPointerException
	at java.base/java.util.HashMap.computeIfPresent(Unknown Source)
	at Example.main(Example.java:11)

Conclusion

In this Java Tutorial, we have learnt the syntax of Java HashMap.computeIfPresent() function, and also learnt how to use this function with the help of examples.