Added in API level 24
java.util.stream
Classes to support functional-style operations on streams of elements, such as map-reduce transformations on collections. For example:int sum = widgets.stream()
.filter(b -> b.getColor() == RED)
.mapToInt(b -> b.getWeight())
.sum();
Here we use widgets, a Collection<Widget>,
as a source for a stream, and then perform a filter-map-reduce on the stream
to obtain the sum of the weights of the red widgets. (Summation is an
example of a reduction
operation.)
The key abstraction introduced in this package is stream. The
classes Stream, IntStream,
LongStream, and DoubleStream
are streams over objects and the primitive int, long, and
double types. Streams differ from collections in several ways:
- No storage. A stream is not a data structure that stores elements; instead, it conveys elements from a source such as a data structure, an array, a generator function, or an I/O channel, through a pipeline of computational operations.
- Functional in nature. An operation on a stream produces a result,
but does not modify its source. For example, filtering a
Streamobtained from a collection produces a newStreamwithout the filtered elements, rather than removing elements from the source collection. - Laziness-seeking. Many stream operations, such as filtering, mapping,
or duplicate removal, can be implemented lazily, exposing opportunities
for optimization. For example, "find the first
Stringwith three consecutive vowels" need not examine all the input strings. Stream operations are di