JDK-8023340 : j.u.Collections.emptyList() is inconsistent in not supporting the "set" operation via set(..) and replaceAll(..)
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.util:collections
  • Affected Version: 8
  • Priority: P2
  • Status: Closed
  • Resolution: Fixed
  • Submitted: 2013-08-20
  • Updated: 2017-05-17
  • Resolved: 2013-09-27
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 8
8 b112Fixed
Related Reports
Relates :  
Description
Consider the following code sample:

---
        try {
            Collections.emptyList().set(10, new Object());
        } catch (UnsupportedOperationException e) {
            System.err.println("UnsupportedOperationException thrown, telling that the set operation is not supported by this list");
        }
        Collections.emptyList().replaceAll(e -> e);
---

The output will be just

UnsupportedOperationException thrown, telling that the set operation is not supported by this list



On one hand empty list doesn't throw IndexOutOfBoundsException but throws UOE when calling set explicitly.

On the other hand it allows to call replaceAll() operation.

The following JCK tests will fail:

api/java_util/Collections/empty/EmptyListReplaceAll.html#EmptyListReplaceAll[elementSettingNotSupported]              
api/java_util/Collections/empty/EMPTY_LISTReplaceAll.html#EMPTY_LISTReplaceAll[elementSettingNotSupported]            
api/java_util/Collections/empty/EMPTY_LISTReplaceAll.html#EMPTY_LISTReplaceAll[elementSettingNotSupported_passingNull]
api/java_util/Collections/empty/EmptyListReplaceAll.html#EmptyListReplaceAll[elementSettingNotSupported_passingNull]  





Comments
Verified doc changes. Mentioned jck tests need update and is tracked by JCK-7301567
22-10-2013

I have added "for some element" to indicate that if there are no elements then the UOE may not be thrown.
05-09-2013

There are several other issues with a similar flavour all resulting from the fact that the Collections implementations don't provide overrides for some defaulted methods. We are considering taking the stance that we will only override methods where we can improve upon the behaviour of the default. We are still considering this response for this type of issue in general.
26-08-2013