-
CSR
-
Resolution: Approved
-
P3
-
None
-
behavioral
-
low
-
-
Java API
-
Implementation
Summary
An unmodifiable Map with one entry uses a different implementation that behaves inconsistently compared to those with zero or two or more entries. It is also inconsistent with other Map implementations that disallow nulls.
Problem
Most operations on unmodifiable collections that involve null will throw NullPointerException. In particular, get(null) on an unmodifiable map with zero or two or more entries will throw NPE. On unmodifiable maps of all sizes, containsKey(null) and containsValue(null) throw NPE, and unmodifiable Lists and Sets throw NPE in response to contains(null). Other map implementations that disallow nulls, such as ConcurrentHashMap and Hashtable, throw NPE for all of these cases. The reason is that nulls are disallowed in these collections. Calling get(null) or contains*(null) is likely to be a programming error. If such methods were to return normally instead of throwing, it might let the error go undetected for longer.
Calling get(null) on a one-entry unmodifiable map returns null. This is clearly inconsistent with all of the above behaviors.
Solution
The one-entry unmodifiable Map implementation should be changed to throw NPE in response to get(null).
Specification
No specification change; this is a behavioral change only.
- csr of
-
JDK-8221924 get(null) on single-entry unmodifiable Map returns null instead of throwing NPE
- Closed