Suppose you have a lazily-populated Iterator
which you would like to convert to a Stream
. It can be assumed that the number of elements in the Iterator
is finite.
It is possible to instruct the Iterator
to skip elements (via a skip
method on the implementation) to avoid unecessary element generation, but all elements of the Iterator
must have been either skipped or generated once the Stream
has had a terminal operation called on it.
I am aware of:
StreamSupport.stream(
Spliterators.spliteratorUnknownSize(
theIterator, Spliterator.ORDERED), false);
for generating a Stream
from an Iterator
, however this does not provide a means to skip or ensure consumption of all elements.
Is there any basic Stream
implementations which would allow for this via reasonably simple extension?
Delegation seems messy given the sheer number of methods of the Stream
interface with no promise as to which will call which others as part of their internal implementation, and all the JDK implementations one could use as a starting point (at least in the JDK I'm using) are package-private and I assume there's good reason for that (e.g. they don't want me extending them...).