Java Reference
In-Depth Information
Intermediate
Stream
operations
filter
Results in a stream containing only the elements that satisfy a condition.
distinct
Results in a stream containing only the unique elements.
limit
Results in a stream with the specified number of elements from the beginning
of the original stream.
map
Results in a stream in which each element of the original stream is mapped to
a new value (possibly of a different type)—e.g., mapping numeric values to
the squares of the numeric values. The new stream has the same number of
elements as the original stream.
sorted
Results in a stream in which the elements are in sorted order. The new stream
has the same number of elements as the original stream.
Fig. 17.3
|
Common intermediate
Stream
operations.
Ter minal
Stream
operations
forEach
Performs processing on every element in a stream (e.g., display each element).
Reduction operations
—Take all values in the stream and return a single value
average
Calculates the
average
of the elements in a numeric stream.
count
Returns the
number of elements
in the stream.
max
Locates the
largest
value in a numeric stream.
min
Locates the
smallest
value in a numeric stream.
reduce
Reduces the elements of a collection to a
single value
using an associative accumu-
lation function (e.g., a lambda that adds two elements).
Mutable reduction operations
—Create a container (such as a collection or
StringBuilder
)
collect
Creates a
new collection
of elements containing the results of the stream's prior
operations.
toArray
Creates an
array
containing the results of the stream's prior operations.
Search operations
findFirst
Finds the
first
stream element based on the prior intermediate operations; immedi-
ately terminates processing of the stream pipeline once such an element is found.
findAny
Finds
any
stream element based on the prior intermediate operations; immediately
terminates processing of the stream pipeline once such an element is found.
anyMatch
Determines whether
any
stream elements match a specified condition; immedi-
ately terminates processing of the stream pipeline if an element matches.
allMatch
Determines whether
all
of the elements in the stream match a specified condition.
Fig. 17.4
|
Common terminal
Stream
operations.
Stream in File Processing vs. Stream in Functional Programming
Throughout this chapter, we use the term
stream
in the context of functional program-
ming—this is not the same concept as the I/O streams we discussed in Chapter 15, Files,