In Java, computeIfPresent(key, remappingFunction) runs the function only if key is in the map with a non-null value. The function receives the key and the old value, and its result replaces the old value.
If the function returns null, the mapping is removed. For a missing key, or a key mapped to null, nothing happens and the method returns null.
An instance method of java.util.HashMap (class since Java 1.2): a hash table of key/value pairs with no guaranteed order, one null key, any number of null values, and no thread safety. computeIfPresent was added in Java 8 as a default method of Map; HashMap overrides it.
Syntax
Pass the key and a lambda of (key, oldValue).
Java
Map<String, Integer> salary = new HashMap<>();
salary.put("Asha", 3000);
salary.computeIfPresent("Asha", (name, s) -> s + 500); // 3500
salary.computeIfPresent("Zoe", (name, s) -> s + 500); // null, not added
Call
What it does
map.computeIfPresent(k, (key, v) -> v + 1)
Increment an existing counter only.
map.computeIfPresent(k, (key, v) -> v > 1 ? v - 1 : null)
Count down and remove at zero.
map.computeIfAbsent(k, f)
The opposite case.
map.replace(k, v)
Replace an existing value without a function.
Inside the function the old value is never null, so you can call methods on it without a null check.
Library declarations
Java
// java.util.HashMap<K, V> (Java 8+)
public V computeIfPresent(K key,
BiFunction<? super K, ? super V, ? extends V> remappingFunction)
// Default method in Map<K, V>, overridden by HashMap
Parameters
Parameter
Description
key (K)
The key to update.
remappingFunction
Receives the key and its non-null value; returns the new value or null to remove. Must not be null.
Return Value
Return Value
Description
V
The new value now stored for key.
null
The key was absent or mapped to null, or the function returned null and the key was removed.
Key found. Asha is in the map with 3000, so the lambda runs and 3500 is stored and returned.
Key not found. Zoe is not an employee, so the lambda never runs and the call returns null.
No accidental insert. Unlike compute, there is no null case to handle, and Zoe is not added.
Others untouched. Ben's salary is unchanged, as the sorted printout shows.
Example 2 — Lose a Life and Leave the Game at Zero
Each hit takes one life; returning null removes a player who has none left.
Java
import java.util.HashMap;
import java.util.Map;
public class HashMapComputeIfPresentLives {
public static void main(String[] args) {
Map<String, Integer> lives = new HashMap<>();
lives.put("p1", 2);
lives.put("p2", 1);
String[] hits = {"p2", "p1", "p2", "p1"};
for (String player : hits) {
Integer left = lives.computeIfPresent(player, (k, v) -> v > 1 ? v - 1 : null);
System.out.println(player + " hit, returned " + left + ", in game: " + lives.containsKey(player));
}
System.out.println("still playing: " + lives);
}
}
Output
p2 hit, returned null, in game: false
p1 hit, returned 1, in game: true
p2 hit, returned null, in game: false
p1 hit, returned null, in game: false
still playing: {}
How It Works
Last life. p2 has 1 life, so v > 1 is false, the lambda returns null, and p2 is removed.
One life left. p1 drops from 2 to 1 and stays in the game.
Already gone. The second hit on p2 finds no key, so nothing runs; the return value is null just like a removal, which is why the loop also prints containsKey.
Empty at the end. p1's second hit removes p1 too, and the map prints as {}.
Example 3 — A null Value Skips the Function
Format one draft using its key; a draft mapped to null is skipped instead of causing a NullPointerException.