In Java, putIfAbsent(key, value) stores value under key only when the key has no mapping or is mapped to null. An existing non-null value is never overwritten.
It returns the current value when one exists, and null when the new value was stored. That makes "add unless taken" a single call instead of a containsKey check followed by put.
An instance method of java.util.HashMap (class since Java 1.2): a hash table of key/value pairs with no guaranteed order, one null key, any number of null values, and no thread safety. putIfAbsent was added in Java 8 as a default method of Map; HashMap overrides it with a single lookup.
Syntax
Pass the key and the value to store if the key is free.
Free names. neo and trin are not taken yet, so both values are stored and each call returns null.
Taken name. The third call finds neo already mapped and returns the existing address instead of storing the new one.
Return value as a signal. Checking for null tells whether the registration succeeded, with no separate containsKey.
No overwrite. The map still has two users, and neo keeps the first address.
Example 2 — A null Value Is Treated as Absent
One plan has a null quota, one has a real quota, and one is missing.
Java
import java.util.HashMap;
import java.util.Map;
public class HashMapPutIfAbsentNull {
public static void main(String[] args) {
Map<String, Integer> quota = new HashMap<>();
quota.put("free", null);
quota.put("pro", 100);
Integer a = quota.putIfAbsent("free", 10);
Integer b = quota.putIfAbsent("pro", 999);
Integer c = quota.putIfAbsent("team", 500);
System.out.println("free: returned " + a + ", now " + quota.get("free"));
System.out.println("pro: returned " + b + ", now " + quota.get("pro"));
System.out.println("team: returned " + c + ", now " + quota.get("team"));
}
}
Output
free: returned null, now 10
pro: returned 100, now 100
team: returned null, now 500
How It Works
null gets filled. "free" is a key, but its value is null, so putIfAbsent treats it as absent and stores 10.
Real value kept. "pro" already has 100, so 999 is ignored and 100 is returned.
Missing key added. "team" does not exist, so 500 is stored and the call returns null.
Same return for two cases. Both "free" and "team" returned null, meaning "the new value was stored", even though one key existed before.
Example 3 — putIfAbsent Creates Values Even When Unused
Group pets by type twice and count how many lists get created each way.
Java
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class HashMapPutIfAbsentEager {
static int created = 0;
static List<String> newList() {
created++;
return new ArrayList<>();
}
public static void main(String[] args) {
String[] pets = {"cat:Tom", "dog:Rex", "cat:Kit", "cat:Mo"};
Map<String, List<String>> eager = new HashMap<>();
for (String p : pets) {
String[] parts = p.split(":");
eager.putIfAbsent(parts[0], newList());
eager.get(parts[0]).add(parts[1]);
}
System.out.println("putIfAbsent: lists created = " + created + ", cats = " + eager.get("cat"));
created = 0;
Map<String, List<String>> lazy = new HashMap<>();
for (String p : pets) {
String[] parts = p.split(":");
lazy.computeIfAbsent(parts[0], k -> newList()).add(parts[1]);
}
System.out.println("computeIfAbsent: lists created = " + created + ", cats = " + lazy.get("cat"));
}
}
Output
putIfAbsent: lists created = 4, cats = [Tom, Kit, Mo]
computeIfAbsent: lists created = 2, cats = [Tom, Kit, Mo]
How It Works
Argument built first. Java evaluates newList() before calling putIfAbsent, so a list is created for all four pets, even when the key already has one.
Wasted lists. Only two of those four lists (one for cats, one for dogs) are stored; the other two are thrown away.
Lazy alternative.computeIfAbsent calls the lambda only for a missing key, so exactly two lists are created.
Same grouping. Both maps end with the cats Tom, Kit, and Mo in input order; only the amount of wasted work differs.
Frequently Asked Questions
The existing value if the key already has a non-null value; otherwise null, after storing the new value.
Yes. A key mapped to null is treated as absent, so the new value is stored.
putIfAbsent() takes a ready-made value, which is always evaluated. computeIfAbsent() takes a function that runs only when the key is missing, and returns the current value.
No. HashMap is not thread-safe. Use ConcurrentHashMap for atomic putIfAbsent across threads.