Java Reference
In-Depth Information
Table 13-1. List of Commonly Used Stream Operations Supported by the Streams API
Operation
Type
Description
Returns a stream consisting of the distinct elements of this stream. Elements e1
and e2 are considered equal if e1.equals(e2) returns true.
Distinct
Intermediate
filter
Intermediate
Returns a stream consisting of the elements of this stream that match the
specified predicate.
flatMap
Intermediate
Returns a stream consisting of the results of applying the specified function
to the elements of this stream. The function produces a stream for each input
element and the output streams are flattened. Performs one-to-many mapping.
limit
Intermediate
Returns a stream consisting of the elements of this stream, truncated to be no
longer than the specified size.
map
Intermediate
Returns a stream consisting of the results of applying the specified function to
the elements of this stream. Performs one-to-one mapping.
peek
Intermediate
Returns a stream whose elements consist of this stream. It applies the specified
action as it consumes elements of this stream. It is mainly used for debugging
purposes.
Discards the first n elements of the stream and returns the remaining stream.
If this stream contains fewer than n elements, an empty stream is returned.
skip
Intermediate
sorted
Intermediate
Returns a stream consisting of the elements of this stream, sorted according to
natural order or the specified Comparator . For an ordered stream, the sort is
stable.
allMatch
Terminal
Returns true if all elements in the stream match the specified predicate, false
otherwise. Returns true if the stream is empty.
anyMatch
Terminal
Returns true if any element in the stream matches the specified predicate, false
otherwise. Returns false if the stream is empty.
Returns any element from the stream. An empty Optional object is for an empty
stream.
findAny
Terminal
findFirst
Terminal
Returns the first element of the stream. For an ordered stream, it returns the first
element in the encounter order; for an unordered stream, it returns any element.
noneMatch
Terminal
Returns true if no elements in the stream match the specified predicate, false
otherwise. Returns true if the stream is empty.
forEach
Terminal
Applies an action for each element in the stream.
reduce
Terminal
Applies a reduction operation to computes a single value from the stream.
Debugging a Stream Pipeline
You apply a sequence of operations on a stream. Each operation transforms the elements of the input stream either
producing another stream or a result. Sometimes you may need to look at the elements of the streams as they pass
through the pipeline. You can do so by using the peek(Consumer<? super T> action) method of the Stream<T>
interface that is meant only for debugging purposes. It produces a stream after applying an action on each input
element. The IntStream , LongStream , and DoubleStream also contain a peek() method that takes a IntConsumer ,
a LongConsumer , and a DoubleConsumer as an argument. Typically, you use a lambda expression with the peek()
method to log messages describing elements being processed.
 
 
Search WWH ::




Custom Search