Java HashMap computeIfPresent() Method
Last Updated :
15 Jan, 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);
}
}
OutputHashMap 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:
- Key: The key whose presence in the map is to be tested.
- BiFunction: A function to compute a new value for the given key.
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);
}
}
OutputHashMap 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:
Similar Reads
Java HashMap computeIfAbsent() Method In Java, the computeIfAbsent() method of the HashMap class is used to compute and insert a value for a specific key to a HashMap only if the key does not already have a value.Example 1: Here, the computeIfAbsent() adds a value for a key if it is not present in the map and does nothing if the key is
3 min read
HashMap compute() Method in Java 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 n
4 min read
Java HashMap containsValue() Method In Java, the containsValue() method of the HashMap class is used to check whether a particular value is being mapped by a single or more than one key in the HashMap. Example 1: This example demonstrates how the containsValue() method works when String values are mapped to integer keys.Java// Java pr
2 min read
HashMap entrySet() Method in Java The entrySet() method of the HashMap class in Java is used to create a set view of the mappings contained in the HashMap. This method allows us to iterate over the key-value pairs in the map or convert them into a set.Example 1: Here, we will use the entrySet() method to view the mappings in a HashM
2 min read
Java HashMap getOrDefault() Method When we work with Java's HashMap, one common problem occurs that is dealing with missing keys. If we try to fetch a value for a key that does not exist, we get "null" and sometimes it may throw NullPointerException. To solve this problem, Java provides a method called getOrDefault() of the HashMap c
4 min read
Hashtable computeIfAbsent() method in Java with Examples The computeIfAbsent(Key, Function) method of Hashtable class which allows you to compute value of a mapping for specified key if key is not already associated with a value (or is mapped to null). If mapping function of this method returns null, then no mapping is recorded. If the remapping function
2 min read