What is the EnumMap.computeIfAbsent() method in Java?
EnumMap is similar to Map, except that EnumMap only takes the Enum type as key. Also, all of the keys must be from a single enum type. For further details, refer here.
The EnumMap.computeIfAbsent() method computes a new value for a specified key, only if the key is not already present in the EnumMap object.
Syntax
public V computeIfAbsent(K key, Function<? super K,? extends V> mappingFunction)
Parameters
key: The key for which the new value is to be computed.
mappingFunction: The mapping function is only called if the mapping for the key is not present.
-
This is a function that takes one argument as input and returns a value.
-
The value returned from the
mappingFunctionwill be updated for the passedkey. -
If
nullis returned from themappingFunction, then no entry will be added.
Return value
-
If the
keyis not present and a new entry is added when thecomputeIfAbsent()method is called, then the new value mapped with the specified key is returned. -
If the
keyis already present, then the value associated with the specifiedkeyis returned.
Code
The below code demonstrates how to use the computeIfAbsent() method.
import java.util.EnumMap;class ComputeIfAbsent {enum Subject {MATHS, SCIENCE, PROGRAMMING, ECONOMICS};public static void main( String args[] ) {EnumMap<Subject, Integer> map = new EnumMap<>(Subject.class);map.put(Subject.MATHS, 50);map.put(Subject.SCIENCE, 60);map.put(Subject.PROGRAMMING, 70);Integer returnVal = map.computeIfAbsent(Subject.ECONOMICS, (key) ->{System.out.println( "\nMapping function called");return 80;});System.out.println("The return value from the computeIfAbsent for key Economics is " + returnVal);System.out.println( "\nAfter calling computeIfAbsent for key Economics , map is\n" + map);returnVal = map.computeIfAbsent(Subject.MATHS, (key) ->{System.out.println( "Mapping function called");return 30;});System.out.println("\nThe return value from the computeIfAbsent for key Maths is " + returnVal);System.out.println( "\nAfter calling computeIfAbsent for key Maths , map is\n" + map);}}
-
In lines 3-5, we create an
Enumfor the Subjects with the nameSubject. -
In line 7, we create an
EnumMapwith the namemap. -
In lines 8-10, we add the following three entries to the
map.
Subject.MATHS - 50
Subject.SCIENCE - 60
Subject.PROGRAMMING - 70
-
In line 12, we then call the
computeIfAbsent()method on the createdmapwith theSubject.ECONOMICSkey. ThecomputeIfAbsent()method will check if theSubject.ECONOMICSkey is not present in themap. In our case, it is not present in themap, so the mapping function will be executed, which adds a new entry to themapwith the following data: -
key:Subject.ECONOMICS -
value: The value returned from the mapping function.
Now, the map will be:
Subject.MATHS - 50
Subject.SCIENCE - 60
Subject.PROGRAMMING - 70
Subject.ECONOMICS - 80
- In line 19, we called the
computeIfAbsent()method on the createdmapwith theSubject.MATHSkey. ThecomputeIfAbsent()method will check if the key is not present in themap. In our case, theSubject.MATHSkey is already present in the map, so the mapping function will not be executed.