-
Enhancement
-
Resolution: Unresolved
-
P4
-
None
-
None
-
generic
-
generic
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 ----------
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 ----------