JDK-8023339 : j.u.Arrays.asList().removeIf() is "lazy" in throwing UOE
  • 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 :  
Relates :  
Description
java.util.Arrays.asList() says:

"Returns a fixed-size list backed by the specified array."

This is confirmed by UOE thrown on an attempt to do array.remove(index).
However removeIf() operation throws UOE only if a real attempt to remove was done while the spec on removeIf() is more strict:

"UnsupportedOperationException - if the remove method is not supported by this collection's iterator"

Please see the following code sample:

---
        final List<Integer> integers = Arrays.asList(1, 2, 3);
        try {
            integers.remove(0);
        } catch (UnsupportedOperationException e) {
            System.err.println("Confirmed: UnsupportedOperationException thrown, removal not supported.");
        }

        System.err.println("Passing alwaysFalse predicate:");
        integers.removeIf(e -> false);
        System.err.println("OK");

        System.err.println("Passing alwaysTrue predicate:");
        integers.removeIf(e -> true);
        System.err.println("OK");
---


The output will be:

Confirmed: UnsupportedOperationException thrown, removal not supported.
Passing alwaysFalse predicate:
OK
Passing alwaysTrue predicate:
Exception in thread "main" java.lang.UnsupportedOperationException
	at java.util.AbstractList.remove(AbstractList.java:161)
	at java.util.AbstractList$Itr.remove(AbstractList.java:374)
	at java.util.Collection.removeIf(Collection.java:407)
	at java.util.Arrays$ArrayList.removeIf(Arrays.java)

The following JCK test will fail due to this:

api/java_util/Arrays/AsListRemoveIf.html#AsListRemoveIf[elementRemovalNotSupported]