HashTable putIfAbsent() method in Java with Examples

The putIfAbsent(Key, value) method of Hashtable class which allows to map a value to a given key if given key is not associated with a value or mapped to null. A null value is returned if such key-value set is already present in the HashMap.

Syntax:

public V putIfAbsent(K key, V value)

Parameters: This method accepts two parameters:

  • key: specifies the key to which the specified value is to be mapped if key is not associated with any value.
  • value: specifies the value to be mapped to the specified key.

Returns: This method returns the existing value mapped to the key and returns null if no value is previously mapped to the key.

Exception: This method throws:

  • NullPointerException: when the specified parameters are null.

Below programs illustrate the putIfAbsent(Key, value) method:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// putIfAbsent(key, value) method.
  
import java.util.*;
  
public class GFG {
  
    // Main method
    public static void main(String[] args)
    {
  
        // crete 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());
  
        // Inserting non-existing key with value
        // using putIfAbsent method
        String retValue
            = String.valueOf(table
                                 .putIfAbsent("Booklet", 2500));
  
        // Print the returned value
        System.out.println("Returned value "
                           + "for Key 'Booklet' is: "
                           + retValue);
  
        // print new mapping
        System.out.println("hashTable: "
                           + table);
  
        // Inserting existing key with value
        // using putIfAbsent method
        retValue
            = String.valueOf(table
                                 .putIfAbsent("Book", 4500));
  
        // Print the returned value
        System.out.println("Returned value"
                           + " for key 'Book' is: "
                           + retValue);
  
        // print new mapping
        System.out.println("hashTable: "
                           + table);
    }
}

chevron_right


Output:

hashTable: {Book=500, Mobile=5000, Pen=10, Clothes=400}
Returned value for Key 'Booklet' is: null
hashTable: {Book=500, Mobile=5000, Pen=10, Clothes=400, Booklet=2500}
Returned value for key 'Book' is: 500
hashTable: {Book=500, Mobile=5000, Pen=10, Clothes=400, Booklet=2500}

Program 2: To Show NullPointerException

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// putIfAbsent(key, value) method.
  
import java.util.*;
  
public class GFG {
  
    // Main method
    public static void main(String[] args)
    {
  
        // crete 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());
  
        try {
  
            table.putIfAbsent(null, "8");
        }
        catch (NullPointerException e) {
  
            System.out.println("Exception: " + e);
        }
    }
}

chevron_right


Output:

hashTable: {3=1000RS, 2=500RS, 1=100RS}
Exception: java.lang.NullPointerException

References: https://docs.oracle.com/javase/8/docs/api/java/util/Hashtable.html#putIfAbsent-K-V-



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.