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, iteratorspliteratorstream

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

Play code: Rept.it

|

Be Updated

Only articles are shared on Medium

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