Card 07/ 09
All 9 cards
GotchaDifficulty: Advanced1 min
Why a null Key Is Fine in One Map and Fatal in Another
Code that has worked for two years throws the first time somebody swaps a HashMap for a TreeMap to get sorted output. The only change was the word after new.
Map<String, Integer> sorted = new TreeMap<>();
sorted.put(null, 1);Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.lang.Comparable.compareTo(Object)" because "k1" is nullA HashMap handles a null key as a special case and stores it in the first bucket. A TreeMap has to compare the key against the ones already there, and there is nothing sensible to compare null with, so it refuses.
| Class | Null key | Null values |
|---|---|---|
HashMap | One | Any number |
LinkedHashMap | One | Any number |
TreeMap | No — throws | Any number |
ConcurrentHashMap | No — throws | No — throws |
Map.of(...) | No — throws | No — throws |
Keep null out of maps as a habit rather than checking the table each time. A key that might be missing is what getOrDefault is for, and a value that might not exist is what Optional is for.