Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
communitycreator

What is the EnumMap.compute() method in Java?

Programming Bytes

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

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.

EnumMap is similar to Map except that EnumMap only takes the Enum type 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 BiFunction that takes two arguments as input and returns a single value.
    • The non-null value returned from the BiFunction is 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 null from the BiFunction, 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.

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 value
EnumMap<Subject, Integer> map = new EnumMap<>(Subject.class);
// add three entries
map.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 key
Integer 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 map
System.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);
}
}
Example for compute method

Explanation

In the code above:

  • We create an EnumMap in 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 the map for the Subject.MATHS key. The Bifunction computes a new value for the Maths key and the compute() function made it the new key.

  • From line 25-38, we call the compute() method on the map for the Subject.ECONOMICS key. There is no mapping associated with the Economics key. So the oldVal will be sent as null to the BiFunction. Inside the BiFunction if the value is null by default 10 will be returned. As the result of the compute method, a new mapping with key Subject.ECONOMICS and value 10 will be added.

RELATED TAGS

java
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring