Scrivio Interview library
ConceptFoundations6 min read · updated 12 November 2026

How does HashMap handle a null key and null values, and why do ConcurrentHashMap and Hashtable reject them?

HashMap allows one null key, which it treats as having hash 0 so it always lives in bucket 0, and any number of null values. Because of that, get(key) returning null is ambiguous: the key may be missing or mapped to null, so we use containsKey or getOrDefault when the difference matters. ConcurrentHashMap rejects null keys and values because in a concurrent map we cannot call containsKey and then get atomically, so null would make absent and null indistinguishable; Hashtable rejects them because it calls key.hashCode() and checks the value directly, and TreeMap with natural ordering throws for a null key.

Why this question is asked

Null handling is one of those details that does not matter until we swap one map for another and the application starts throwing NullPointerException in places that used to work. Interviewers ask about it because it reveals whether we know that Map implementations differ in their contracts, and because the reason behind ConcurrentHashMap's rule is a neat piece of concurrent design. In this answer, we will recap how HashMap stores a null key, see why a null result from get() is ambiguous and how to resolve it, understand why ConcurrentHashMap forbids nulls, and finish with Hashtable, TreeMap and the immutable maps.

1A Quick Recap: Where HashMap Puts a null Key

HashMap normally calls key.hashCode() to pick a bucket. Calling a method on null would throw, so HashMap's internal hash() method checks for null first and returns 0. With a hash of 0, the bucket index (n - 1) & 0 is always 0, so the null key lives in bucket 0, alongside any other keys whose hashes happen to land there. When comparing keys in a bucket, HashMap checks identity first, so null is found without calling equals() on it.

Java
Map<String, String> m = new HashMap<>();
m.put(null, "first");
m.put(null, "second"); // same key: replaces the value
m.put("a", null);
m.put("b", null); // many null values are fine
System.out.println(m.size()); // 3
System.out.println(m.get(null)); // second

So there can be only one null key, because all null keys are the same key, but values are just stored data, and null values are allowed in any number.

2Why get() Returning null Is Ambiguous

Now look at what get() tells us. It returns the value for the key, or null if there is no mapping. When values can be null, those two cases look the same.

Java
Map<String, String> m = new HashMap<>();
m.put("a", null);
System.out.println(m.get("a")); // null: mapped to null
System.out.println(m.get("z")); // null: not present at all
System.out.println(m.containsKey("a")); // true
System.out.println(m.containsKey("z")); // false
System.out.println(m.getOrDefault("a", "none")); // null: the key is present, so its value is returned
System.out.println(m.getOrDefault("z", "none")); // none: the key is absent

Notice that getOrDefault returns the default only when the key is absent, not when it is mapped to null, so it also tells the cases apart. In a single-threaded map, the two-step pattern if (m.containsKey(k)) return m.get(k) is perfectly safe, because nothing can change the map between the two calls.

3Why ConcurrentHashMap Rejects Nulls

In a map shared by many threads, that two-step pattern breaks. Between our containsKey() and our get(), another thread may remove or add the key. So if get() returned null, we could never find out reliably whether the key was absent or mapped to null: any follow-up check sees a different moment in time.

ConcurrentHashMap's designers solved this by removing the ambiguity entirely. Neither keys nor values may be null, so a null from get() always means no mapping, and the result of a single atomic call is enough. This also lets the implementation use null internally to mean empty or not yet set, which keeps its lock-free reads simple. Both put(null, v) and put(k, null) throw NullPointerException, and so do get(null) and containsKey(null).

Step throughWhy a null value would be ambiguous in a concurrent map
Reader threadShared mapWriter thread
get(k)
null
put(k, v) or remove(k)
containsKey(k) to find out what null meant
answer describes a different moment

5 messages in order. Play them, or step through them one at a time.

With no null values allowed, the first answer is already unambiguous.

4Hashtable, TreeMap and the Immutable Maps

Hashtable, the legacy synchronized map from Java 1.0, also rejects both. Its put() explicitly throws NullPointerException for a null value, and it calls key.hashCode() directly, which throws for a null key. That behaviour predates the concurrency argument; it is simply how the code was written, and it was kept for compatibility.

TreeMap does not hash at all; it orders keys with compareTo or a Comparator. With natural ordering, comparing null throws NullPointerException, so a null key is rejected, while null values are fine. If we supply a comparator that handles nulls, such as Comparator.nullsFirst(Comparator.naturalOrder()), a null key is allowed and sorts first.

Java
new Hashtable<String, String>().put(null, "v"); // NullPointerException
new Hashtable<String, String>().put("k", null); // NullPointerException
new TreeMap<String, String>().put(null, "v"); // NullPointerException
var t = new TreeMap<String, String>(Comparator.nullsFirst(Comparator.naturalOrder()));
t.put(null, "v"); t.put("a", "w");
System.out.println(t); // {null=v, a=w}
Null support across Map implementations
Mapnull keynull values
HashMap, LinkedHashMapone allowedallowed
TreeMap, natural orderingNullPointerExceptionallowed
TreeMap, null-friendly comparatorallowedallowed
ConcurrentHashMapNullPointerExceptionNullPointerException
HashtableNullPointerExceptionNullPointerException
Map.of, Map.copyOfNullPointerExceptionNullPointerException

5Follow-up questions

What does Map.of do with null keys or values?

It throws NullPointerException at creation if any key or value is null, and Map.copyOf does the same for the source map's entries. The immutable maps go further: even querying with null, as in Map.of("a", 1).containsKey(null) or get(null), throws NullPointerException, while a HashMap would just return false or null. That surprises code that probes maps with possibly-null keys.

Is it good practice to store null values in a HashMap?

Usually not. It is legal, but it forces every reader to remember the ambiguity and to use containsKey or getOrDefault, and it makes migrating to ConcurrentHashMap or Map.copyOf harder later. Modelling absence explicitly, by not storing the key, by using Optional values, or by using a sentinel object, is clearer.

Why does the null key go in bucket 0 and not a separate field?

Java 7's HashMap had a special putForNullKey path. Java 8 simplified it by letting hash(null) return 0, so the null key goes through the normal code in bucket 0 and the only special case is a null check before calling hashCode().

6Conclusion

In this answer, we saw that HashMap stores one null key in bucket 0, because hash(null) is defined as 0, and allows any number of null values, which makes a null result from get() ambiguous; containsKey or getOrDefault resolves it in a single-threaded map. ConcurrentHashMap rejects null keys and values because a concurrent map cannot check and read atomically, so only a null-free design makes a null result unambiguous. Hashtable throws because it calls key.hashCode() and checks values directly, TreeMap with natural ordering throws for a null key unless a null-friendly comparator is used, and Map.of and Map.copyOf reject nulls entirely.

What to say in the interviewAbout 90 seconds aloud

Open with

HashMap allows one null key and any number of null values. Its internal hash method checks for null and returns 0, so the null key always goes to bucket 0, and there is only one because all nulls are the same key.

Then, if they want more

The catch with null values is that get returning null is ambiguous: either the key is missing or it is mapped to null. In a normal HashMap I resolve that with containsKey, or with getOrDefault, which returns the default only when the key is absent. ConcurrentHashMap does not allow nulls for keys or values, and the reason is concurrency.

If null values were allowed and get returned null, I would have to call containsKey to find out what it meant, but another thread could change the map between the two calls, so the answer would describe a different moment.

By forbidding null, a null from get always means no mapping, in one atomic call, and the implementation can also use null internally to mean empty. Hashtable rejects both for a simpler reason: its put throws for a null value, and it calls key.hashCode directly, which throws for a null key.

TreeMap with natural ordering throws NullPointerException for a null key because it cannot compare it, but a comparator such as Comparator.nullsFirst allows one, and null values are fine. Finally, Map.of and Map.copyOf reject null keys and values, and even containsKey(null) throws on them.

About this answer

Last updated
12 November 2026
Versions it names
Java 7, Java 8
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "How does HashMap handle a null key and null values, and why do ConcurrentHashMap and...": https://scrivio-interview-library.netlify.app/java/hashmap-internals/913bfb36ec/