Details
-
Bug
-
Resolution: Fixed
-
P2
-
8
-
b112
-
Verified
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]
"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]
Attachments
Issue Links
- relates to
-
JDK-8024291 Rename Collection.removeIf(Predicate) back to removeAll(Predicate)
- Resolved
-
JDK-8001647 default methods for Collections - forEach, removeIf, replaceAll, sort
- Closed