stream pipeline |
Having seen some examples of Stream usage, now its time for official documents of Stream Interface.
public interface Stream<T> extends BaseStream<T,Stream<T>>
A sequence of elements supporting sequential and parallel aggregate operations. The following example illustrates an aggregate operation using
Stream
and IntStream
:
int sum = widgets.stream()
.filter(w -> w.getColor() == RED)
.mapToInt(w -> w.getWeight())
.sum();
In this example, widgets
is a Collection<Widget>
. We create a stream of Widget
objects via Collection.stream()
, filter it to produce a stream containing only the red widgets, and then transform it into a stream of int
values representing the weight of each red widget. Then this stream is summed to produce a total weight.We will study more examples later.