HashMap Null Keys/Values
Fast recall
Quick revision
Core answer
null has no hashCode()) and any number of null values. By contrast, Hashtable and ConcurrentHashMap forbid null keys and values entirely. To tell a null value apart from a missing key, use containsKey().Whether you know HashMap's null rules and how they differ from Hashtable/ConcurrentHashMap
Saying HashMap allows multiple null keys, or using get()==null to test key presence
Explain ConcurrentHashMap bans null so get()==null is unambiguous in concurrent code
Interview-ready guide
Interview answer
Yes, a HashMap can have null keys and null values, but with a limit.
It allows exactly one null key and any number of null values. The single null key is handled as a special case, because null doesn't have a hashCode, so HashMap just stores it in bucket zero. And since keys are unique, if you put a second entry with a null key, it simply overwrites the first one.
Null values, on the other hand, have no such limit — you can map as many keys as you want to null. This is one of the classic differences between the map types. Hashtable, which is the old legacy class, doesn't allow null keys or null values at all; it throws a NullPointerException.
ConcurrentHashMap also forbids null keys and values, and the reason is interesting: in concurrent code, if get returns null, it has to mean the key is absent. If null values were allowed, a null result would be ambiguous — it could mean absent or mapped to null — and that ambiguity is unsafe across threads, so they banned null outright.
One practical tip that interviewers like: with a HashMap, don't test whether a key exists by checking if get returns null, because that can't distinguish a missing key from a key that's genuinely mapped to null. Use containsKey instead, or getOrDefault if you want a fallback. And note that a TreeMap with natural ordering also rejects null keys, because it has to compare them to sort.
HashMap Null Keys/Values
Direct Answer
Yes — HashMap allows one null key (stored in bucket 0, since null has no hashCode()) and any number of null values. By contrast, Hashtable and ConcurrentHashMap forbid null keys and values entirely. To tell a null value apart from a missing key, use containsKey().
Code Example
Watch how HashMap accepts a null key and null values, while ConcurrentHashMap rejects them outright.
import java.util.*;
import java.util.concurrent.*;
public class NullKeysDemo {
public static void main(String[] args) {
Map<String, String> map = new HashMap<>();
map.put(null, "v"); // OK: one null key
map.put(null, "w"); // overwrites -> value now "w"
map.put("a", null); // OK: null value
map.put("b", null); // OK: many null values
// Distinguish missing key from null value:
boolean present = map.containsKey("a"); // true
String v = map.get("missing"); // null -> but key absent
// ConcurrentHashMap forbids null entirely:
Map<String, String> chm = new ConcurrentHashMap<>();
// chm.put(null, "x"); // NullPointerException
}
}
The containsKey("a") check is the reliable way to ask 'is this key present?' — get() alone cannot tell you.
Common Mistakes & Pitfalls
- 'Multiple null keys.' Only one — a second null-key put overwrites the first.
get() == nullto test presence. That can't tell 'absent' from 'value is null'. UsecontainsKey().- Assuming ConcurrentHashMap allows null. It doesn't, by design.
- Putting null in a
TreeMap. A natural-ordered TreeMap throws on a null key (it must compare it).
Check your understanding
Answer these without looking back at the article.
- How do you distinguish between a null value and a missing key in HashMap?
- Why doesn't ConcurrentHashMap allow null keys?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.