Java Map computeIfPresent()
February 20, 2020
The computeIfPresent
is the default method of java.util.Map
and has been introduced in Java 8. The computeIfPresent
method computes a specified mapping function for the given key and its associated value, and then updates the value for that key if the value for the specified key is present and non-null. Find the method declaration from Java doc.
default V computeIfPresent(K key, BiFunction<? super K,? super V,? extends V> remappingFunction)
The remappingFunction is the specified mapping function of Java
BiFunction
type to compute a new value.
The
computeIfPresent
method returns new value associated with the specified key, or null if none.
The
computeIfPresent
method works as following.
1. If specified key is associated with a not null value and new value computed by specified mapping function is also not null, in this case the
computeIfPresent
method will put the new value for the specified key.
2. If specified key is associated with a not null value and new value computed by specified mapping function is null, in this case the
computeIfPresent
method will remove the entry for the specified key.
3. If specified key is associated with a null value and new value computed by specified mapping function is not null, in this case the
computeIfPresent
method will do nothing.
4. If specified key is associated with a not null value and mapping function throws exception, in this case there will be no change in the
Map
.
Example-1
Specified key is associated with a not null valueand new value computed by specified mapping function is also not null.
In this case the
computeIfPresent
method will put the new value for the specified key.
ComputeIfPresent1.java
import java.util.HashMap; import java.util.Map; public class ComputeIfPresent1 { public static void main(String[] args) { Map<Integer, String> cityMap = new HashMap<>(); cityMap.put(101, "Varanasi"); cityMap.put(102, "Prayag"); String newValue = cityMap.computeIfPresent(102, (k, v) -> v != null ? v.concat("raj") : null); System.out.println(newValue); System.out.println(cityMap); } }
Prayagraj {101=Varanasi, 102=Prayagraj}
computeIfPresent
method as following.
ComputeIfPresent11.java
import java.util.HashMap; import java.util.Map; import java.util.function.BiFunction; public class ComputeIfPresent11 { public static void main(String[] args) { Map<Integer, String> cityMap = new HashMap<>(); cityMap.put(101, "Varanasi"); cityMap.put(102, "Prayag"); BiFunction<Integer, String, String> remappingFunction = (k, v) -> v != null ? v.concat("raj") : null; int key = 102; String oldValue = cityMap.get(key); String newValue = remappingFunction.apply(key, oldValue); if (cityMap.get(key) != null) { if (newValue != null) cityMap.put(key, newValue); else cityMap.remove(key); } System.out.println(newValue); System.out.println(cityMap); } }
Prayagraj {101=Varanasi, 102=Prayagraj}
Example-2
Specified key is associated with a not null valueand new value computed by specified mapping function is null.
In this case the
computeIfPresent
method will remove the entry for the specified key.
ComputeIfPresent2.java
import java.util.HashMap; import java.util.Map; public class ComputeIfPresent2 { public static void main(String[] args) { Map<Integer, String> cityMap = new HashMap<>(); cityMap.put(101, "Varanasi"); cityMap.put(102, "Prayag"); cityMap.computeIfPresent(102, (k, v) -> null); System.out.println(cityMap); } }
{101=Varanasi}
Example-3
Specified key is associated with a null valueand new value computed by specified mapping function is not null.
In this case the
computeIfPresent
method will do nothing. There will be no change in the Map
.
ComputeIfPresent3.java
import java.util.HashMap; import java.util.Map; public class ComputeIfPresent3 { public static void main(String[] args) { Map<Integer, String> cityMap = new HashMap<>(); cityMap.put(101, "Varanasi"); cityMap.put(102, null); cityMap.computeIfPresent(102, (k, v) -> "Noida"); System.out.println(cityMap); } }
{101=Varanasi, 102=null}
Example-4
Specified key is associated with a not null valueand mapping function throws exception.
In this case there will be no change in the
Map
.
ComputeIfPresent4.java
import java.util.HashMap; import java.util.Map; public class ComputeIfPresent4 { public static void main(String[] args) { Map<Integer, String> cityMap = new HashMap<>(); cityMap.put(101, "Varanasi"); cityMap.put(102, "Noida"); String newVal = null; try { cityMap.computeIfPresent(102, (k, v) -> newVal.concat("Prayag")); //throws exception } catch (Throwable e) { System.out.println(e); } System.out.println(cityMap); } }
java.lang.NullPointerException {101=Varanasi, 102=Noida}