The computeIfPresent(Key, BiFunction) method of HashMap class which allows you to compute value of a mapping for specified key if key is already associated with a value (or is mapped to null).
- If mapping function of this method returns null, the mapping is removed.
- If the remapping function throws an exception, the exception is rethrown, and the mapping is left unchanged.
- During computation, modification this map using this method is not allowed.
Syntax:
public Object computeIfPresent(Object key,
BiFunction remappingFunction)
Parameters: This method accepts two parameters:
- key: key with which the value is to be associated.
- remappingFunction: function to do the operation on value.
Returns: This method returns new remapped value associated with the specified key, or null if mapping returns null.
Below programs illustrate the computeIfPresent(Key, BiFunction) method:
Example 1: This example demonstrates the case when the key is not in the hashmap.
// Java program to demonstrate // computeIfPresent(Key, BiFunction) method. import java.util.concurrent.*; import java.util.*; public class GFG { public static void main(String[] args) { // Create a HashMap and add some values HashMap<String, Integer> wordCount = new HashMap<>(); wordCount.put("Geeks", 1); wordCount.put("for", 2); wordCount.put("geeks", 3); // print HashMap details System.out.println("Hashmap before operation :\n " + wordCount); // provide new value for keys which is present // using computeIfPresent method wordCount.computeIfPresent("Geek", (key, val) -> val + 100); // print new mapping System.out.println("HashMap after operation :\n " + wordCount); } } |
Hashmap before operation :
{geeks=3, Geeks=1, for=2}
HashMap after operation :
{geeks=3, Geeks=1, for=2}
Example 2: This example demonstrates the case when the key is present in the hashmap.
// Java program to demonstrate // computeIfPresent(Key, BiFunction) method. import java.util.concurrent.*; import java.util.*; public class GFG { public static void main(String[] args) { // Create a HashMap and add some values HashMap<String, Integer> wordCount = new HashMap<>(); wordCount.put("Geeks", 1); wordCount.put("for", 2); wordCount.put("geeks", 3); // print HashMap details System.out.println("Hashmap before operation :\n " + wordCount); // provide new value for keys which is present // using computeIfPresent method wordCount.computeIfPresent("for", (key, val) -> val + 1); // print new mapping System.out.println("HashMap after operation :\n " + wordCount); } } |
Hashmap before operation :
{geeks=3, Geeks=1, for=2}
HashMap after operation :
{geeks=3, Geeks=1, for=3}
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- HashMap merge(key, value, BiFunction) method in Java with Examples
- HashMap replaceAll(BiFunction) method in Java with Examples
- Properties computeIfPresent(Key, BiFunction) method in Java with Examples
- Properties compute(Key, BiFunction) method in Java with Examples
- Java | BiFunction Interface methods - apply() and addThen()
- HashMap compute() method in Java with Examples
- HashMap computeIfAbsent() method in Java with Examples
- HashMap replace(key, oldValue, newValue) method in Java with Examples
- HashMap replace(key, value) method in Java with Examples
- HashMap putIfAbsent(key, value) method in Java with Examples
- HashMap forEach(BiConsumer) method in Java with Examples
- HashMap getOrDefault(key, defaultValue) method in Java with Examples
- HashMap Class Methods in Java with Examples | Set 1 (put(), get(), isEmpty() and size())
- Hashmap methods in Java with Examples | Set 2 (keySet(), values(), containsKey()..)
- Load Factor in HashMap in Java with Examples
- HashMap in Java with Examples
- HashMap clear() Method in Java
- HashMap containsKey() Method in Java
- HashMap clone() Method in Java
- HashMap containsValue() Method in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

