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.
public V computeIfAbsent(K key, Function<? super K,? extends V> mappingFunction)
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 mappingFunction
will be updated for the passed key
.
If null
is returned from the mappingFunction
, then no entry will be added.
If the key
is not present and a new entry is added when the computeIfAbsent()
method is called, then the new value mapped with the specified key is returned.
If the key
is already present, then the value associated with the specified key
is returned.
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 Enum
for the Subjects with the name Subject
.
In line 7, we create an EnumMap
with the name map
.
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 created map
with the Subject.ECONOMICS
key. The computeIfAbsent()
method will check if the Subject.ECONOMICS
key is not present in the map
. In our case, it is not present in the map
, so the mapping function will be executed, which adds a new entry to the map
with 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
computeIfAbsent()
method on the created map
with the Subject.MATHS
key. The computeIfAbsent()
method will check if the key is not present in the map
. In our case, the Subject.MATHS
key is already present in the map, so the mapping function will not be executed.