In Java 8, the Stream
class does not have any method to wrap a an Iterable
.
Instead, I am obtaining the Spliterator
from the Iterable
and then obtaining a Stream
from StreamSupport
like this:
boolean parallel = true; StreamSupport.stream(spliterator(), parallel) .filter(Row::isEmpty) .collect(Collectors.toList()) .forEach(this::deleteRow);
Is there some other way of generating Stream
operations on an Iterable
that I am missing?
CopyTo(Stream) Reads the bytes from the current stream and writes them to another stream. Both streams positions are advanced by the number of bytes copied.
stream() works in sequence on a single thread with the println() operation. list. parallelStream(), on the other hand, is processed in parallel, taking full advantage of the underlying multicore environment. The interesting aspect is in the output of the preceding program.
My similar question got marked as duplicate, but here is the helper methods I've used to avoid some of the boilerplate:
public static <T> Stream<T> stream(Iterable<T> in) { return StreamSupport.stream(in.spliterator(), false); } public static <T> Stream<T> parallelStream(Iterable<T> in) { return StreamSupport.stream(in.spliterator(), true); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With