Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
hashmap
compute
communitycreator

How to use HashMap.computeIfAbsent method in Java

Programming Bytes

The HashMap.computeIfAbsent() method computes a new value for a specified key only if the key is not already present in the HashMap.

Syntax

hashmap.computeIfAbsent(K key,Function mappingFunction)

Arguments

key: The key for which the new value must 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.

Return value

  • 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.

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.computeIfAbsent("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( "After calling computeIfAbsent for key Economics , map is\n" + map);

        returnVal = map.computeIfAbsent("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( "After calling computeIfAbsent for key Maths , map is\n" + map);


    }
}

In the code above, we created a HashMap with the following entries.

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

We then called the computeIfPresent method on the the created map with the Economics key. The computeIfAbsent will check if the Economics key is not present on 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:

  • key: “Economics”
  • value: The value returned from the mapping function

will be added to the map.

Now, the map will be:

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

Next, we called the computeIfAbsent method on the created map with the Maths key. The computeIfAbsent will check if the Maths key is not present on the map. In our case, the Maths key is already present in the map, so the mapping function will not be executed.

RELATED TAGS

java
hashmap
compute
communitycreator
RELATED COURSES

View all Courses

Keep Exploring