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

LinkedHashSet unbalanced splitting when using Spliterator

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Unresolved
    • Icon: P4 P4
    • None
    • None
    • core-libs

      A DESCRIPTION OF THE PROBLEM :
      When trySplit() is called on an IteratorSpliterator, instead of splitting, it will delegate all of its elements to the newly-returned Spliterator. This new Spliterator will then split correctly from then on. This works, I guess, but is strange and tripped me up while testing a Spliterator wrapper I created. This may be hard to fix elegantly, as the new Spliterator is of a different type (ArraySpliterator), meaning IteratorSpliterator would need to copy its functionality after splitting.

      PROPOSAL:
      - A disclaimer that this is allowed behavior in the Spliterator documentation would be appreciated: in which data structures can we expect a balanced split
      - If possible, a method that can indicate that behavior before splitting, or a flag after splitting

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM:
      Compile and run the code given in the source block.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      50
      25
      25
      ACTUAL -
      0
      50
      50

      ---------- BEGIN SOURCE ----------
      import java.util.*;

      public class Main {
          public static void main(String[] args) {
            Set<Integer> set = new LinkedHashSet<>();
            for (int i = 0; i < 100; i++)
              set.add(i);
            
            Spliterator<Integer> spliterator1 = set.spliterator();
            Spliterator<Integer> spliterator2 = spliterator1.trySplit();
            Spliterator<Integer> spliterator3 = spliterator2.trySplit();
            
            System.out.println(spliterator1.estimateSize());
            System.out.println(spliterator2.estimateSize());
            System.out.println(spliterator3.estimateSize());
        }
      }
      ---------- END SOURCE ----------

        1. Main.java
          0.6 kB
          Patricia Tavares

            Unassigned Unassigned
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: