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 API which accepts spliterator. You can convert an iterator to spliterator through Spliterators.spliteratorUnknownSize, then spliterator to stream.

In gist, iteratorspliteratorstream

  Stream<Integer> stream =
      Spliterators.spliteratorUnknownSize(iterator, Spliterator.ORDERED),

Play code:

This site uses third-party cookies to gather website usage and performance data.