Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to perform Stream functions on an Iterable? [duplicate]

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?

like image 608
The Coordinator Avatar asked Dec 01 '13 08:12

The Coordinator


People also ask

What is the function used to duplicate a stream?

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.

What is the difference between stream () and parallelStream ()?

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.


1 Answers

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); } 
like image 144
Scott B Avatar answered Sep 21 '22 20:09

Scott B