The Spliterator
looks more like a class created for other more sophisticated classes to use. The basic logic is in trySplit()
, attempting to divide the remaining elements in two Spliterator
: the 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), so split it in two with the trySplit()
, and prints the new sizes.
Assuming that sizes can be accurately printed, there are two possibilities of what will be observed:
- A mistake of the kind
NullPointerException
when calling a method in sit2
, because it was not possible to divide the Spliterator
;
sit
and sit2
, combined, has the size that sit
had originally.
I personally do not recommend trying to use the Spliterator
directly, because optimizing this kind of thing is difficult -- so even the Spliterator
has things like collection type, estimated size, sequential processing of the remainder, the fact of the Spliterator
not be thread-safe, etc.
If you want parallelism, use the Stream
parallel. It is very easy to use, and all optimization logic is already embedded.
Daniel, you know what criteria the trySplit() method uses to make that division?
– Geison Santos
@Geisonsantos Depends on the implementation. On something like a
ArrayList
, he probably splits it in half.– Daniel C. Sobral
Sometimes you have to access one
Spliterator
, how to do parallel processing from aIterable
: https://answall.com/a/345434/64969; in practice, however, I only saw it as a means to callStreamSupport
, no major changes related to this. Let’s say that, although I have accessed, I did not actually use theSpliterator
in response, and it remains a black box for me– Jefferson Quesado