lauhin
10/15/2019 - 6:23 PM

Java iterator to stream

Java iterator to stream

De methode


public static <T> Stream<T> iteratorToStream(final Iterator<T> iterator, final boolean parallell) {
    Iterable<T> iterable = () -> iterator;
    return StreamSupport.stream(iterable.spliterator(), parallell);
}

De uitvoering


try (Stream<???> stream = Utils.<???>iteratorToStream(anIterator, true)) {
    //via try() sluit de stream vanzelf
}