Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
hashmap
communitycreator

What is the HashMap.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.

Answers Code

The HashMap.computeIfPresent() method will compute a new value for the specified key only if the key is already present in the HashMap.

Syntax

hashmap.computeIfPresent(K key, BiFunction remappingFunction)

Arguments

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

remappingFunction:

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

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

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

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

Return value

  • In the case where key is present, the new updated value for the key is returned.

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

Example

import java.util.HashMap;
class merge {
public static void main( String args[] ) {
HashMap<String, Integer> map = new HashMap();
map.put("Maths", 50);
map.put("Science", 60);
map.put("Programming", 70);
System.out.println( "The map is - " + map);
Integer returnVal = map.computeIfPresent("Maths", (key, oldVal) ->{
System.out.println( "ReMapping function called with key " + key + " value " + oldVal );
return 80;
});
System.out.println("After calling computeIfPresent for key Maths");
System.out.println("\nThe return value is " + returnVal);
System.out.println( "The map is\n" + map);
returnVal = map.computeIfPresent("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);
}
}

In the code above, we have created a HashMap with the entries given below.

"Maths" - 50
"Science" - 60
"Programming" - 70

The program then calls the computeIfAbsent method on the created map with the Maths key. computeIfAbsent 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 not be executed.

Now, the map will be:

"Maths" - 80
"Science" - 60
"Programming" - 70

Finally, the program calls the computeIfAbsent method on the created map with the Economics key. computeIfAbsent will check if the key Economics is present in the map. In our case, the Economics key is not present in the map, so the mapping function will be executed and a new entry with the Economics key and value 80 will be added to the map.

RELATED TAGS

java
hashmap
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.

Answers Code
Keep Exploring