Java HashMap putIfAbsent() Method
Last Updated :
22 Jan, 2025
The putIfAbsent() method of the HashMap class in Java is used to insert a key-value pair into the map only if the key is not present in the map or is mapped to null. The existing value remains unchanged if the key is already associated with a value.
Example 1: In this example, we will demonstrate the basic usage of the putIfAbsent() method.
Java
import java.util.HashMap;
public class Geeks {
public static void main(String[] args)
{
// Create a HashMap
HashMap<Integer, String> hm = new HashMap<>();
// Insert some values
hm.put(1, "Geek1");
hm.put(2, "Geek2");
// Use putIfAbsent
System.out.println("Before putIfAbsent(): " + hm);
// Key does not exist, so the value is inserted
hm.putIfAbsent(3, "Geek3");
// Key exists, so the value is not inserted
hm.putIfAbsent(1, "Geek4");
System.out.println("After putIfAbsent(): " + hm);
}
}
OutputBefore putIfAbsent(): {1=Geek1, 2=Geek2}
After putIfAbsent(): {1=Geek1, 2=Geek2, 3=Geek3}
Explanation: In the above example, the Key 3 is not present, so it gets added with the value "Geek3". The Key 1 already exists, so the value "Geek1" remains unchanged.
Syntax of putIfAbsent() Method
default V putIfAbsent(K key, V value)
V: The return type of the method. It represents the type of the value associated with the key in the HashMap.
Parameters:
- key: The key with which the specified value is to be associated.
- value: The value to be associated with the specified key.
Return Type: This method return previous value associated with the specified key or return null if there was no mapping for the key.
Key Points:
- The method does not overwrite existing mappings.
- If the key is mapped to null, the new value is inserted.
- Returns null if the key was absent; otherwise, returns the existing value.
Example 2: In this example, we will see how putIfAbsent() method handles null values and return values.
Java
// Handling Null Values and return values
import java.util.HashMap;
public class Geeks {
public static void main(String[] args) {
// Create a HashMap and add some values,
// including a null value
HashMap<String, Integer> hm = new HashMap<>();
hm.put("A", 100);
hm.put("B", 200);
hm.put("C", null);
// Display the original map
System.out.println("Original HashMap: " + hm);
// Insert a value for a key that is not present
Integer res1 = hm.putIfAbsent("D", 400);
// Insert a value for a key that is mapped to null
Integer res2 = hm.putIfAbsent("C", 300);
// Attempt to insert a value for an existing key
Integer res3 = hm.putIfAbsent("A", 500);
System.out.println("Result for Key D (absent): " + res1);
System.out.println("Result for Key C (null): " + res2);
System.out.println("Result for Key A (existing): " + res3);
System.out.println("Updated HashMap: " + hm);
}
}
OutputOriginal HashMap: {A=100, B=200, C=null}
Result for Key D (absent): null
Result for Key C (null): null
Result for Key A (existing): 100
Updated HashMap: {A=100, B=200, C=300, D=400}
Explanation: In the above example, the Key D is added because it is not present in the map. The Key C is mapped to null, so it is updated with the value 300. The Key A exists with a value of 100, so no change occurs.