What is the EnumMap.compute() method in Java?
The EnumMap.compute() method in Java computes a new value for the specified key using a mapping function provided by the user. If the passed key is not present then no operation is performed and null is returned.
EnumMapis similar toMapexcept thatEnumMaponly takes theEnumtype as key. Also, all the keys must be from a single enum type. For further details refer here.
Syntax
public V compute(K key, BiFunction remappingFunction)
Parameters
-
key: The key for which the new value is computed. -
remappingFunction:- A
BiFunctionthat takes two arguments as input and returns a single value. - The non-null value returned from the
BiFunctionis updated as the value of the passed key of the map. In case if the mapping for the key is not present, a new entry is added. - If we return
nullfrom theBiFunction, the mapping for the key is removed. - If the function itself throws an exception, then the
compute()function also throws an exception, and the current mapping is left unchanged.
- A
Return
The compute() method returns the new value associated with the key.
Code
The code below demonstrates how to use the compute() method.
import java.util.EnumMap;class Compute {enum Subject {MATHS, SCIENCE, PROGRAMMING, ECONOMICS};public static void main( String args[] ) {// create a new map which can have Subject type Enum as key and Integer as valueEnumMap<Subject, Integer> map = new EnumMap<>(Subject.class);// add three entriesmap.put(Subject.MATHS, 50);map.put(Subject.SCIENCE, 60);map.put(Subject.PROGRAMMING, 70);System.out.println( "The map is - " + map);System.out.println( "\nCalling compute function for key Maths");//call compute method for already present keyInteger newVal = map.compute(Subject.MATHS, (key, oldVal) -> { return oldVal + 10; });System.out.println("\nThe return value is " + newVal);System.out.println( "The map is - " + map);//call compute method for key which is nott present in the mapSystem.out.println( "\n---------------\n");System.out.println( "Calling compute function for key Economics\n");newVal =map.compute(Subject.ECONOMICS,(key, oldVal) -> {System.out.print("Inside BiFunction: The key is ");System.out.print(key);System.out.print(". The value is ");System.out.println(oldVal + ".");if(oldVal != null) {return oldVal + 10;}return 10;});System.out.println("\nThe return value is " + newVal);System.out.println( "The map is - " + map);}}
Explanation
In the code above:
- We create an
EnumMapin line 8-12 with the entries given below:
Subject.MATHS - 50
Subject.SCIENCE - 60
Subject.PROGRAMMING - 70
-
In line 18, we call the
compute()method on themapfor theSubject.MATHSkey. TheBifunctioncomputes a new value for theMathskey and thecompute()function made it the new key. -
From line 25-38, we call the
compute()method on themapfor theSubject.ECONOMICSkey. There is no mapping associated with theEconomicskey. So theoldValwill be sent asnullto the BiFunction. Inside the BiFunction if the value isnullby default10will be returned. As the result of thecomputemethod, a new mapping with keySubject.ECONOMICSand value10will be added.