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

j.u.Arrays.asList().removeIf() is "lazy" in throwing UOE

    XMLWordPrintable

Details

    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]




      Attachments

        Issue Links

          Activity

            People

              mduigou Mike Duigou
              dbessono Dmitry Bessonov
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: