Java Map computeIfAbsent() Example
February 19, 2020
The computeIfAbsent
is the default method of java.util.Map
and has been introduced in Java 8. The computeIfAbsent
method works when the value associated with specified key is not available or null and in this case the computeIfAbsent
method put the new value for that key computed by given mapping function. Find the method declaration from Java doc.
default V computeIfAbsent(K key, Function<? super K,? extends V> mappingFunction)
The mappingFunction is the
java.util.function.Function
type that compute a value for mapping.
The
computeIfAbsent
returns existing or new value computed by given mapping function.
The
computeIfAbsent
method works as following.
1. If the specified key is not already associated with a value and new value calculated by mapping function is not null, in this case the
computeIfAbsent
method will put the new value for the specified key.
2. If the specified key is not already associated with a value and new value calculated by mapping function is null, in this case the
computeIfAbsent
method will not put the new value for the specified key.
3. If the specified key is already associated with a value and new value calculated by mapping function is not null, in this case the
computeIfAbsent
method will not put the new value for the specified key.
4. If the specified key is associated with a null value and new value calculated by mapping function is not null, in this case the
computeIfAbsent
method will put the new value for the specified key.
Example-1
Specified key is not associated with a valueand new value calculated by mapping function is not null.
In this case the
computeIfAbsent
method will put the new value for the specified key.
ComputeIfAbsent1.java
import java.util.HashMap; import java.util.Map; public class ComputeIfAbsent1 { public static void main(String[] args) { Map<Integer, String> cityMap = new HashMap<>(); cityMap.put(101, "Varanasi"); cityMap.put(102, "Prayag"); String value = cityMap.computeIfAbsent(103, k -> "Noida"); System.out.println(value); System.out.println(cityMap); } }
Noida {101=Varanasi, 102=Prayag, 103=Noida}
computeIfAbsent
method as following.
ComputeIfAbsent11.java
import java.util.HashMap; import java.util.Map; import java.util.function.Function; public class ComputeIfAbsent11 { public static void main(String[] args) { Map<Integer, String> cityMap = new HashMap<>(); cityMap.put(101, "Varanasi"); cityMap.put(102, "Prayag"); Function<Integer, String> mappingFunction = k -> "Noida"; int key = 103; String value = mappingFunction.apply(key); if (cityMap.get(key) == null) { if (value != null) cityMap.put(key, value); } System.out.println(value); System.out.println(cityMap); } }
Noida {101=Varanasi, 102=Prayag, 103=Noida}
computeIfAbsent
method.
ComputeIfAbsent12.java
import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class ComputeIfAbsent12 { public static void main(String[] args) { Map<String, List<String>> map = new HashMap<>(); List<String> countries = new ArrayList<>(); countries.add("Bharat"); map.put("countries", countries); map.computeIfAbsent("capitals", k -> new ArrayList<>()).add("Delhi"); System.out.println(map); } }
{capitals=[Delhi], countries=[Bharat]}
Example-2
Specified key is not associated with a valueand new value calculated by mapping function is null.
In this case the
computeIfAbsent
method will not put the new value for the specified key.
ComputeIfAbsent2.java
import java.util.HashMap; import java.util.Map; public class ComputeIfAbsent2 { public static void main(String[] args) { Map<Integer, String> cityMap = new HashMap<>(); cityMap.put(101, "Varanasi"); cityMap.put(102, "Prayag"); cityMap.computeIfAbsent(103, k -> null); System.out.println(cityMap); } }
{101=Varanasi, 102=Prayag}
Example-3
Specified key is associated with a valueand new value calculated by mapping function is not null.
In this case the
computeIfAbsent
method will not put the new value for the specified key. There will be no change in Map
.
ComputeIfAbsent3.java
import java.util.HashMap; import java.util.Map; public class ComputeIfAbsent3 { public static void main(String[] args) { Map<Integer, String> cityMap = new HashMap<>(); cityMap.put(101, "Varanasi"); cityMap.put(102, "Prayag"); cityMap.computeIfAbsent(102, k -> "Prayagraj"); System.out.println(cityMap); } }
{101=Varanasi, 102=Prayag}
Example-4
Specified key is associated with a null valueand new value calculated by mapping function is not null.
In this case the
computeIfAbsent
method will put the new value for the specified key.
ComputeIfAbsent4.java
import java.util.HashMap; import java.util.Map; public class ComputeIfAbsent4 { public static void main(String[] args) { Map<Integer, String> cityMap = new HashMap<>(); cityMap.put(101, "Varanasi"); cityMap.put(102, null); cityMap.computeIfAbsent(102, k -> "Prayagraj"); System.out.println(cityMap); } }
{101=Varanasi, 102=Prayagraj}