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

Map.of() derived view collection mutators should throw UnsupportedOperationException

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: P4 P4
    • 25
    • None
    • core-libs
    • None

      My colleague Sorin Basca reports:

      A DESCRIPTION OF THE PROBLEM:

      Calling Map.of().clear() results in an UnsupportedOperationException. However, calling Map.of().entrySet().clear() just returns doing nothing. Both calls should behave in the same way.

      ROOT CAUSE:

      Calling Map.of() returns an ImmutableCollections.EMPTY_MAP which returns a MapN object. MapN.entrySet() returns an object that does not override clear(), which means the default clear is used - it iterates and removes items until iterator.hasNext() is false - so the function just returns without removing anything. However, if calling MapN.clear() directly, this will throw an UnsupportedOperationException.

      POSSIBLE SOLUTION:

      Override clear() inside the entrySet class returned from MapN to also throw UnsupportedOperationException.

            cushon Liam Miller-Cushon
            cushon Liam Miller-Cushon
            Votes:
            1 Vote for this issue
            Watchers:
            6 Start watching this issue

              Created:
              Updated: