-
Type:
Bug
-
Resolution: Unresolved
-
Priority:
P4
-
Affects Version/s: 11
-
Component/s: 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.
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.
- csr for
-
JDK-8329284 Map.of() derived view collection mutators should throw UnsupportedOperationException
-
- Provisional
-
- relates to
-
JDK-8316493 Remove the caching fields in AbstractMap
-
- In Progress
-
- links to
-
Review(master)
openjdk/jdk/18522