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

Spliterator behavior for LinkedList contradicts the spec of Spliterator.trySplit() method

    XMLWordPrintable

Details

    Description

      (issue is reproducible with JDK8b87)

      Consider the following code sample:

      import java.util.ArrayList;
      import java.util.Arrays;
      import java.util.Spliterator;

      public class SplittingLinkedList {

          public static void main(String[] args) {

              final Spliterator spliterator = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5)).spliterator();
              System.out.println("initial spliterator size = " + spliterator.estimateSize());
              final Spliterator nextSpliterator = spliterator.trySplit();
              System.out.println("next splitertor size = " + nextSpliterator.estimateSize());
              System.out.println("initial spliterator size = " + spliterator.estimateSize());

          }
      }

      From the output it could be seen that the first attempt to split consumes all the initial spliterator:


      initial spliterator size = 5
      next splitertor size = 5
      initial spliterator size = 0


      This behavior contradicts the spec for Spliterator.trySplit():
      http://download.java.net/jdk8/docs/api/java/util/Spliterator.html#trySplit()

      "... the value reported for estimateSize() before splitting, if not already zero or Long.MAX_VALUE, must, after splitting, be greater than estimateSize() for this and the returned Spliterator ... "

      Either the specification or behavior of the implementation should be corrected.

      The following JCK (not yet integrated) tests fail due to this issue:

      api/java_util/concurrent/ArrayBlockingQueue/SpliteratorGeneral.html#SpliteratorGeneral[estimateSize_decreasingWithTrySplitCalls]
      api/java_util/LinkedHashSet/SpliteratorGeneral.html#SpliteratorGeneral[estimateSize_decreasingWithTrySplitCalls]
      api/java_util/LinkedList/SpliteratorGeneral.html#SpliteratorGeneral[estimateSize_decreasingWithTrySplitCalls]
      api/java_util/Spliterators/SpliteratorFromCollection.html#SpliteratorFromCollection[estimateSize_decreasingWithTrySplitCalls]



      Attachments

        Issue Links

          Activity

            People

              psandoz Paul Sandoz
              dbessono Dmitry Bessonov
              Votes:
              0 Vote for this issue
              Watchers:
              5 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: