Last Updated : 06 Aug, 2025
The compute(Key, BiFunction) method of the HashMap class in Java is used to update or compute a value for a specific key. It tries to compute a mapping for the specified key and its current mapped value (or null if there is no current mapping). If the remapping function passed to compute() returns null, the mapping is removed from the HashMap (or remains absent if initially absent).
This method is useful when we need to automatically update the value for a given key in the HashMap.
Example 1: Here, we will use the compute() method to update the values in a HashMap of String keys and String values.
Java
// Update the values in a HashMap of
// String keys and String values
import java.util.*;
public class GFG {
public static void main(String[] args) {
// Create a Map and add some values
Map<String, String> m = new HashMap<>();
m.put("Name", "Aman");
m.put("Address", "Kolkata");
// Print the map
System.out.println("Map: " + m);
// Remap the values using
// compute() method
m.compute("Name", (key, val)
-> val.concat(" Singh"));
m.compute("Address", (key, val)
-> val.concat(" West-Bengal"));
// Print new mapping
System.out.println("New Map: " + m);
}
}
Map: {Address=Kolkata, Name=Aman} New Map: {Address=Kolkata West-Bengal, Name=Aman Singh}
Explanation: In the above example, the compute() method is used to append strings to the existing values in the HashMap based on the keys "Name" and "Address".
Syntax of HashMap compute() Methoddefault V
compute(K key,
BiFunction<? super K, ? super V, ?
extends V> remappingFunction)
Parameters:
Returns: The new value associated with the specified key, or null if none.
Exceptions:
Points to Remember:
Mapping to increment a int value of mapping: map.compute(key, (k, v) -> (v == null) ? 1 : v+1)
Example 2: Here, we will use the compute() method to increment values in a HashMap of String keys and Integer values.
Java
// Increment the values in a HashMap of
// String keys and Integer values
import java.util.*;
public class GFG {
public static void main(String[] args) {
// Create a Map and add some values
Map<String, Integer> m = new HashMap<>();
m.put("Key1", 12);
m.put("Key2", 15);
// Print map details
System.out.println("Map: " + m);
// Remap the values
// using compute() method
m.compute("Key1", (key, val)
-> (val == null) ? 1 : val + 1);
m.compute("Key2", (key, val)
-> (val == null) ? 1 : val + 1);
System.out.println("New Map: " + m);
}
}
Map: {Key2=15, Key1=12} New Map: {Key2=16, Key1=13}
Explanation: In the above example, the compute() method increments the integer values in the HashMap for keys "Key1" and "Key2".
Example 3: In this example, we demonstrate how NullPointerException occurs when a null
key is passed to the compute()
method.
// Demonstrate NullPointerException when a
// null key is passed to compute() method
import java.util.*;
public class GFG {
public static void main(String[] args) {
// Create a Map and add some values
Map<String, Integer> m = new HashMap<>();
m.put("Key1", 12);
m.put("Key2", 15);
// Print map details
System.out.println("Map: " + m);
try {
// Pass a null key to compute() method
m.compute(null, (key, value) -> value + 3);
System.out.println("New Map: " + m);
} catch (NullPointerException e) {
System.out.println("Exception: " + e);
}
}
}
Map: {Key2=15, Key1=12} Exception: java.lang.NullPointerException
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4