Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

computeifpresent
enummap
java
communitycreator

What is the EnumMap.computeIfPresent() 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.

Overview

EnumMap is similar to Map, except that EnumMap only takes the Enum type as a key. Also, all the keys must be from a single Enum type. For further details, refer here.

The EnumMap.computeIfPresent() method computes a new value for the specified key only if the key is already present in the map.

Syntax

Let’s view the syntax of the method:

public V computeIfPresent(K key, BiFunction remappingFunction)
computeIfPresent syntax in Java

Arguments

  • key: The key for which the new value is to be computed.

Defining remappingFunction

  • The remappingFunction function is only called if the mapping for the key is present.

  • remappingFunction is a BiFunction that takes two arguments as input and returns a value.

  • The value returned from remappingFunction will be updated for the passed key.

  • If null is returned from remappingFunction, then the entry will be removed.

Return value

  • If key is present, then the new updated value for the key is returned.

  • If the key is not present, then null is returned.

Code

The below code demonstrates how to use the computeIfPresent method.

import java.util.EnumMap;
class ComputeIfPresent {
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.computeIfPresent(Subject.MATHS, (key, oldVal) ->{
System.out.println( "\nReMapping function called with key " + key + " value " + oldVal );
return 80;
});
System.out.println("\nAfter calling computeIfPresent for key Maths");
System.out.println("\nThe return value is " + returnVal);
System.out.println( "The map is\n" + map);
returnVal = map.computeIfPresent(Subject.ECONOMICS, (key, oldVal) ->{
System.out.println( "\n ReMapping function called");
return 80;
});
System.out.println("\nAfter calling computeIfPresent for key Economics");
System.out.print("The return value is ");
System.out.println(returnVal);
System.out.println( "The map is\n" + map);
}
}

Explanation

In the above code, we created an EnumMap with the entries given below.


Subject.MATHS - 50
Subject.SCIENCE - 60
Subject.PROGRAMMING - 70

The program then calls the computeIfPresent method on the created map with the Subject.MATHS key. computeIfPresent will check if the key MATHS is present in the map or not. Since the MATHS key is already present in the map, the mapping function will be executed.

Now, the map will be as follows:


Subject.MATHS - 80
Subject.SCIENCE - 60
Subject.PROGRAMMING - 70

Finally, the program calls the computeIfPresent method on the created map with the Subject.ECONOMICS key, then computeIfPresent will check if the key Subject.ECONOMICS is present in the map.

In our case, the Subject.ECONOMICS key is not present in the map, so the mapping function will not be executed and null is returned.

RELATED TAGS

computeifpresent
enummap
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