Java Reference
In-Depth Information
Simplifying Processing with Streams
Problem
You want to process some data through a pipeline-like mechanism.
Solution
Use a
Stream
class and its operations.
Discussion
Streams
are a new mechanism introduced with Java 8 to allow a collection to send its values
out one at a time through a pipeline-like mechanism where they can be processed in various
ways, with varying degrees of parallelism. There are three types of methods involved with
Stream
s:
▪ Stream-producing methods (see
The Collections Framework
)
▪ Stream-passing methods, which operate on a Stream and return a reference to it, to allow
for “fluent programming” (chained methods calls); examples include
distinct()
,
fil-
ter()
,
limit()
,
map()
,
peek()
,
sorted()
,
unsorted()
, and more
▪ Stream-terminating methods, which conclude a streaming operation; examples include
count()
,
findFirst()
,
max()
,
min()
,
reduce()
,
sum()
, among others