Hashtable computeIfAbsent() method in Java with Examples

The computeIfAbsent(Key, Function) method of Hashtable class which allows you to compute value of a mapping for specified key if key is not already associated with a value (or is mapped to null).

  • If mapping function of this method returns null, then no mapping is recorded.
  • If the remapping function throws an exception, the exception is rethrown, and the no mapping is recorded.
  • During computation, modification this map using this method is not allowed.
  • This method will throw a ConcurrentModificationException if the remapping function modified this map during computation.

Syntax:

public V 
       computeIfAbsent(K key,
             Function<? super K, ? extends V> remappingFunction)

Parameters: This method accepts two parameters:

  • key: key with which the value is to be associated.
  • remappingFunction: function to do the operation on value.

Returns: This method returns current (existing or computed) value associated with the specified key, or null if mapping returns null.

Exception: This method throws:

  • ConcurrentModificationException: if it is detected that the remapping function modified this map.

Below programs illustrate the computeIfAbsent(Key, Function) method:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// computeIfAbsent(Key, Function) method.
  
import java.util.*;
  
public class GFG {
  
    // Main method
    public static void main(String[] args)
    {
  
        // create a table and add some values
        Map<String, Integer> table = new Hashtable<>();
        table.put("Pen", 10);
        table.put("Book", 500);
        table.put("Clothes", 400);
        table.put("Mobile", 5000);
  
        // print map details
        System.out.println("hashTable: "
                           + table.toString());
  
        // provide value for new key which is absent
        // using computeIfAbsent method
        table.computeIfAbsent("newPen", k -> 600);
        table.computeIfAbsent("newBook", k -> 800);
  
        // print new mapping
        System.out.println("new hashTable: "
                           + table);
    }
}

chevron_right


Output:

hashTable: {Book=500, Mobile=5000, Pen=10, Clothes=400}
new hashTable: {newPen=600, Book=500, newBook=800, Mobile=5000, Pen=10, Clothes=400}

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// computeIfAbsent(Key, Function) method.
  
import java.util.*;
  
public class GFG {
  
    // Main method
    public static void main(String[] args)
    {
  
        // create a table and add some values
        Map<Integer, String> table = new Hashtable<>();
        table.put(1, "100RS");
        table.put(2, "500RS");
        table.put(3, "1000RS");
  
        // print map details
        System.out.println("hashTable: "
                           + table.toString());
  
        // provide value for new key which is absent
        // using computeIfAbsent method
        table.computeIfAbsent(4, k -> "600RS");
  
        // this will not effect anything
        // because key 1 is present
        table.computeIfAbsent(1, k -> "800RS");
  
        // print new mapping
        System.out.println("new hashTable: "
                           + table);
    }
}

chevron_right


Output:

hashTable: {3=1000RS, 2=500RS, 1=100RS}
new hashTable: {4=600RS, 3=1000RS, 2=500RS, 1=100RS}

References: https://docs.oracle.com/javase/10/docs/api/java/util/Hashtable.html#computeIfAbsent(K, java.util.function.Function)



My Personal Notes arrow_drop_up

I am a Developer I love to code and bring my ideas alive

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.