While looking into the source code of the WrappingSpliterator::trySplit
, I was very mislead by it's implementation:
@Override
public Spliterator<P_OUT> trySplit() {
if (isParallel && buffer == null && !finished) {
init();
Spliterator<P_IN> split = spliterator.trySplit();
return (split == null) ? null : wrap(split);
}
else
return null;
}
And if you are wondering why this matters, is because for example this:
Arrays.asList(1,2,3,4,5)
.stream()
.filter(x -> x != 1)
.spliterator();
is using it. In my understanding the addition of any intermediate operation to a stream, will cause that code to be triggered.
Basically this method says that unless the stream is parallel, treat this Spliterator as one that can not be split, at all. And this matters to me. In one of my methods (this is how I got to that code), I get a Stream
as input and "parse" it in smaller pieces, manually, with trySplit
. You can think for example that I am trying to do a findLast
from a Stream
.
And this is where my desire to split in smaller chunks is nuked, because as soon as I do:
Spliterator<T> sp = stream.spliterator();
Spliterator<T> prefixSplit = sp.trySplit();
I find out that prefixSplit
is null
, meaning that I basically can't do anything else other than consume the entire sp
with forEachRemaning
.
And this is a bit weird, may be it makes some sense for when filter
is present; because in this case the only way (in my understanding) a Spliterator
could be returned is using some kind of a buffer
, may be even with a predefined size (much like Files::lines
). But why this:
Arrays.asList(1,2,3,4)
.stream()
.sorted()
.spliterator()
.trySplit();
returns null
is something I don't understand. sorted
is a stateful operation that buffers the elements anyway, without actually reducing or increasing their initial number, so at least theoretically this can return something other than null
...
This method may return {@code null} for any reason, * including emptiness, inability to split after traversal has * commenced, data structure constraints, and efficiency * considerations.
– Wisthler Apr 03 '19 at 06:59