Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

hashtable
java
communitycreator

How to use the Hashtable.computeIfPresent method in Java?

Programming Bytes

A HashTable is a collection of key-value pairs. The object to be used as a key should implement the hashCode and equals methods. The key and the value should not be null.

You can read more about the difference between a HashTable and HashMap here.

The computeIfPresent() method

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

Syntax


public V computeIfPresent(K key, BiFunction remappingFunction)

Parameters

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

  • remappingFunction: The function to use to compute the new value for key.

The 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

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

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

Code

import java.util.Hashtable;
class ComputeIfPresent {
    public static void main( String args[] ) {

        Hashtable<String, Integer> map = new Hashtable<>();
        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( "\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("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 code above, we created a Hashtable with the following entries:


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

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

Now, the map will be as follows:


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

Finally, the program calls the computeIfPresent method on the created map with the Economics key.

In our case, the Economics key is not present in the map, so the mapping function will not be executed. Hence, the computeIfPresent method returns null.

RELATED TAGS

hashtable
java
communitycreator
RELATED COURSES

View all Courses

Keep Exploring