HashMap putIfAbsent(key, value) method in Java with Examples

The putIfAbsent(K key, V value) method of HashMap class is used map the specified key with the specified value, only if no such mapping exists in this HashMap instance.

Syntax:

public V putIfAbsent(K key, V value)

Parameters: This method accepts two parameters:



  • key: which is the key with which provided value has to be mapped.
  • value: which is the value which has to be associated with the provided key, if absent.

Return Value: This method returns null (if there was no mapping with the provided key before or it was mapped to a null value) or previous value associated with provided key.

Exceptions: This method throws following exceptions:

  • NullPointerException: if the specified key or value is null, and this map does not support null value.
  • IllegalArgumentException: if the specified key or value is preventing it from being stored in the map.

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)
    {
  
        // create a HashMap and add some values
        HashMap<String, Integer> map
            = new HashMap<>();
        map.put("a", 10000);
        map.put("b", 55000);
        map.put("c", 44300);
        map.put("e", 53200);
  
        // print original map
        System.out.println("HashMap:\n "
                           + map.toString());
  
        // put a new value which is not mapped
        // before in map
        map.putIfAbsent("d", 77633);
  
        // print newly mapped map
        System.out.println("New HashMap:\n "
                           + map);
    }
}

chevron_right


Output:

HashMap:
 {a=10000, b=55000, c=44300, e=53200}
New HashMap:
 {a=10000, b=55000, c=44300, d=77633, e=53200}

Program 2:

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)
    {
  
        // create a HashMap and add some values
        HashMap<String, Integer> map
            = new HashMap<>();
        map.put("a", 10000);
        map.put("b", 55000);
        map.put("c", 44300);
        map.put("e", null);
  
        // print original map
        System.out.println("HashMap:\n "
                           + map.toString());
  
        // put a new value which is not mapped
        // before in map and store the returned
        // value in r1
        Integer r1 = map.putIfAbsent("d", 77633);
  
        // put a new value for key 'e' which is mapped
        // with a null value, and store the returned
        // value in r2
        Integer r2 = map.putIfAbsent("e", 77633);
  
        // print the value of r1
        System.out.println("Value of r1:\n " + r1);
  
        // print the value of r2
        System.out.println("Value of r2:\n " + r2);
  
        // print newly mapped map
        System.out.println("New HashMap:\n "
                           + map);
    }
}

chevron_right


Output:

HashMap:
 {a=10000, b=55000, c=44300, e=null}
Value of r1:
 null
Value of r2:
 null
New HashMap:
 {a=10000, b=55000, c=44300, d=77633, e=77633}

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



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.