Say you want to stream the elements of an iterator; let's use a concrete example of a Scanner
, which implements Iterator<String>
.
Given an Iterator
, say:
// Scanner implements Iterator<String>
Iterator<String> scanner = new Scanner(System.in);
Options to create a Stream<String>
from it are the clunky:
StreamSupport.stream(
Spliterators.spliteratorUnknownSize(scanner, Spliterator.ORDERED), false);
or the slightly more terse, but obtuse:
StreamSupport.stream(
((Iterable<String>) () -> new Scanner(System.in)).spliterator(), false);
Is there a factory method somewhere in the JDK that returns a Stream<T>
given an Iterator<T>
?