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

j.u.Collections.nCopies() instance is not conformant to Collection.removeIf() spec

XMLWordPrintable

      Specification for Collections.nCopies() says:

      "Returns an immutable list consisting of n copies of the specified object."

      However its iterator doesn't throw UOE, but throws ISE and calling of method removeIf(alwaysFalse) doesn't lead to the expected UnsupportedOperationException.

      http://download.java.net/jdk8/docs/api/java/util/Collection.html#removeIf(java.util.function.Predicate)
      "Throws:
      UnsupportedOperationException - if the remove method is not supported by this collection's iterator()"


      Please see the following code sample:

              final List<String> nCopies = Collections.nCopies(3, "a");

              try {
                  nCopies.iterator().remove();
              } catch (UnsupportedOperationException uoe) {
                  System.out.println("OK, remove method is not supported");
              } catch (Exception e) {
                  System.out.println("e = " + e);
              }

              nCopies.removeIf( e -> false);


      The output will be:

      e = java.lang.IllegalStateException

      The following JCK test will fail:
      api/java_util/Collections/ncopies/RemoveIf.html#RemoveIf[elementRemovalNotSupported]





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

              Created:
              Updated:
              Resolved: