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:
// 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); } } |
HashMap:
{a=10000, b=55000, c=44300, e=53200}
New HashMap:
{a=10000, b=55000, c=44300, d=77633, e=53200}
Program 2:
// 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); } } |
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-
Recommended Posts:
- HashMap computeIfAbsent() method in Java with Examples
- HashMap compute() method in Java with Examples
- HashMap replace(key, value) method in Java with Examples
- HashMap computeIfPresent(key, BiFunction) method in Java with Examples
- HashMap replaceAll(BiFunction) method in Java with Examples
- HashMap merge(key, value, BiFunction) method in Java with Examples
- HashMap getOrDefault(key, defaultValue) method in Java with Examples
- HashMap forEach(BiConsumer) method in Java with Examples
- HashMap replace(key, oldValue, newValue) method in Java with Examples
- HashMap in Java with Examples
- Hashmap methods in Java with Examples | Set 2 (keySet(), values(), containsKey()..)
- HashMap Class Methods in Java with Examples | Set 1 (put(), get(), isEmpty() and size())
- HashMap put() Method in Java
- HashMap get() Method in Java
- HashMap clone() Method in Java
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.

