Spliterator
looks like a class created for other more sophisticated classes to use. The basic logic is in trySplit()
, which tries to divide the remaining elements into two Spliterator
: current and a new one, which the method returns.
For example:
void <T> showSize(Spliterator<T> sit) {
System.out.println("Estimated size for sit: "+sit.getExactSizeIfKnown());
Spliterator<T> sit2 = sit.trySplit();
System.out.println("New estimated size for sit: "+sit.getExactSizeIfKnown());
System.out.println("Estimated size for sit2: "+sit2.getExactSizeIfKnown());
return;
}
The method receives a Spliterator
, sit
, prints the size (this may not work), then splits it in two with trySplit()
, and prints the new sizes.
Assuming that the sizes can be printed accurately, there are two possibilities about what will be observed:
An error of type NullPointerException
when calling a method in sit2
, because it was not possible to split Spliterator
;
sit
and sit2
, in addition, has the size that sit
had originally.
I personally do not recommend trying to use Spliterator
directly, since optimizing this type of thing is difficult - so even Spliterator
has things like collection type, estimated size, sequential processing of the rest, fact that Spliterator
is not thread safe, etc.
If you want parallelism, use parallel Stream
. It's very easy to use, and all optimization logic is already built in.