Java HashMap.compute() – Examples

In this tutorial, we will learn about the Java HashMap.compute() function, and learn how to use this function to compute a new value for a specific key, with the help of examples.

compute()

HashMap.compute() computes a mapping for the specified key and its current mapped value (or null if there is no current mapping).

Syntax

The syntax of compute() function is

compute(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 V

Example 1 – compute()

In this example, we will initialize a HashMap with mappings from String to Integer. Using compute() function, we will compute a value for key "C", such that the new value is square of the existing value. Also, we shall do the same computation for key "D".

Java Program

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.compute("C", (k,v) -> v = v*v );
		hashMap.compute("D", (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=16}

Example 2 – compute() for All keys

In this example, we will initialize a HashMap with mappings from String to Integer. Using compute() function, we will replace all the values with their squares using HashMap.forEach() and compute() methods.

Java Program

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.forEach((key, value) -> {
			hashMap.compute(key, (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=4, C=9, D=16}

Example 3 – compute() – Return Value

In this example, we will store the value returned by compute(), 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

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 x = hashMap.compute("C", (k,v) -> v = v*v );
		System.out.println("Return Value : " + x);
	}
}

Output

Return Value : 9

Example 4 – compute() for Key not Present

In this example, we will initialize a HashMap with mappings from String to Integer. We will try to call compute() function for a key "M" that is not present in the HashMap. Since the key is not present in HashMap, compute() should throw java.lang.NullPointerException.

Java Program

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.compute("M", (k,v) -> v = v*v );
		System.out.println("After  compute : " + hashMap);
	}
}

Output

Before compute : {A=1, B=2, C=3, D=4}
Exception in thread "main" java.lang.NullPointerException
	at Example.lambda$0(Example.java:12)
	at java.base/java.util.HashMap.compute(Unknown Source)
	at Example.main(Example.java:12)

Conclusion

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