Java HashMap computeIfPresent() Method (original) (raw)

Last Updated : 12 Jul, 2025

In Java, the **computeIfPresent() method of the HashMap class is used to compute a new value for a specified key if the key is already present in the map and its value is not null. If the key is not present, no action is taken.

**Example 1: In this example, the **computeIfPresent() method **updates the value of the key if it exists in the map.

Java `

// Java program to demonstrates the // working of computeIfPresent() import java.util.HashMap;

public class Geeks { public static void main(String[] args) {

    // Create a HashMap and add some values
    HashMap<String, Integer> m = new HashMap<>();
    m.put("Geeks", 1);
    m.put("for", 2);
    m.put("geeks", 3);

    // print HashMap details
    System.out.println("HashMap before operation :\n" + m);

    // Update value for the key "Geeks" using computeIfPresent
    m.computeIfPresent("Geeks", (key, val) -> val + 100);

    // print new mapping
    System.out.println("HashMap after operation :\n" + m);
}

}

`

Output

HashMap before operation : {geeks=3, Geeks=1, for=2} HashMap after operation : {geeks=3, Geeks=101, for=2}

**Explanation: The above example demonstrates the **computeIfPresent() method by updating the value of the key "Geeks" in a HashMap if it exists. The value is incremented by 100, and the updated HashMap is displayed.

Syntax of computeIfPresent() Method

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

**Parameters:

**Return Type: The new value associated with the key or null if the key was removed.

**Example 2: The below Java program demonstrates that **no change will occur in the map if the key is absent.

Java `

// Java program to demonstrates the working of // computeIfPresent() when the key is absent in the map import java.util.HashMap;

public class Geeks { public static void main(String[] args) {

    // Create a HashMap and add some values
    HashMap<String, Integer> m = new HashMap<>();
    m.put("Geeks", 1);
    m.put("for", 2);
    m.put("geeks", 3);

    // print HashMap details
    System.out.println("HashMap before operation :\n" + m);

    // key "Java" is not present in the map, so no change
    m.computeIfPresent("Java", (key, val) -> val + 100);

    // print new mapping
    System.out.println("HashMap after operation :\n" + m);
}

}

`

Output

HashMap before operation : {geeks=3, Geeks=1, for=2} HashMap after operation : {geeks=3, Geeks=1, for=2}

**Example 3: If we pass null as the remapping function, a NullPointerException will be thrown.

Java `

// Java program to demonstrates // computeIfPresent() throws NullPointerException import java.util.HashMap;

public class Geeks { public static void main(String[] args) { HashMap<String, Integer> m = new HashMap<>(); m.put("Geeks", 10);

    // Passing null as the remapping function
    // throws NullPointerException
    m.computeIfPresent("Geeks", null);
}

}

`

**Output:

Output