Java - Iterator to Stream
There is no direct API available to convert an iterator
to stream
in Stream
interface. However, there is an indirect path available for the same.
There is StreamSupport.stream
API which accepts spliterator
. You can convert an iterator
to spliterator
through Spliterators.spliteratorUnknownSize
, then spliterator
to stream
.
In gist,
iterator
→spliterator
→stream
Stream<Integer> stream = StreamSupport.stream(
Spliterators.spliteratorUnknownSize(iterator, Spliterator.ORDERED),
false);