The putIfAbsent(K key, V value) method of HashMap class is used to map the specified key with the specified value, only if no such key exists (or is mapped to null) 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 current value associated with the provided key.
Exceptions:
This method can throw the 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.
- UnsupportedOperationException – if the put operation is not supported by this map (optional)
- ClassCastException – if the key or value is of an inappropriate type for this map (optional)
Program 1:
// Java program to demonstrate // putIfAbsent(Key, value) method. import java.util.HashMap;
public class TestClass {
// 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 );
System.out.println( "New HashMap:\n " + map);
// try to put a new value with existing key
// before in map
map.putIfAbsent( "d" , 55555 );
// print newly mapped map
System.out.println( "Unchanged HashMap:\n " + map);
}
} |
HashMap: {a=10000, b=55000, c=44300, e=53200} New HashMap: {a=10000, b=55000, c=44300, d=77633, e=53200} Unchanged 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-