Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8221924

get(null) on single-entry unmodifiable Map returns null instead of throwing NPE

XMLWordPrintable

        The unmodifiable maps are supposed to be null-hostile. However, a one-entry unmodifiable map will simply return null when get(null) is called. This is inconsistent with the behavior of other unmodifiable maps that have different numbers of elements.

        Map.of().get(null) ==> throws NPE
        Map.of(1, 2).get(null) ==> returns null
        Map.of(1, 2, 3, 4).get(null) ==> throws NPE

        Moreover, containsKey(null) and containsValue(null) on all unmodifiable maps throw NPE, and contains(null) on unmodifiable lists and sets all throw NPE. Thus, the get(null) behavior should be modified also to throw NPE.

        Thanks to Claes Redestad for spotting this.

        This fix should be backported to 12 and 11 as well.

              smarks Stuart Marks
              smarks Stuart Marks
              Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

                Created:
                Updated:
                Resolved: