Stream Operations OverviewS2C Home « Stream Operations Overview
We have seen several examples of intermediate stream operations such as filter()
and terminal stream operations like count()
in the first two lessons. In this lesson we look at all of the intermediate and terminal stream operations available. We will see coding examples of individual operations, both intermediate and terminal, in subsequent lessons.
Intermediate Operations Top
Once a stream is created from a source we use intermediate operations to process the stream such as filtering the stream using the filter()
method which we used in the Introducing Streams lesson. A stream pipeline doesn't have to have any intermediate stream operations, such as just using the count()
terminal operation on its own, but this rarely makes any sense from a coding perspective. What generally happens is one or more intermediate operations transform the stream pipeline until the terminal operation processes it.
The following table lists all the intermediate stream operations available:
Operation | Return Type | Type or Functional Interface | Description | Code Examples |
---|---|---|---|---|
distinct() stateful-unbounded | Stream<T> | Returns a stream of unique elements. | Numeric Streams | |
filter() | Stream<T> | Predicate<T> | Returns a stream of unique elements. | Introducing Streams Stream Pipelines Finding & Matching |
flatMap() | Stream<R> | Function<T, Stream<R>> | Returns a stream consisting of the results of replacing each element of this stream with the contents of a mapped stream produced by applying the provided mapping function to each element. | Other Stream Creation |
limit() stateful-bounded | Stream<T> | long | Returns a stream consisting of the elements of this stream, truncated to be no longer than maxSize in length. | Stream Pipelines |
map() | Stream<R> | Function<T, R> | Returns a stream consisting of the results of applying the given function to the elements of this stream. | Array Type Streams Finding & Matching Reduction Operations |
skip() stateful-bounded | Stream<T> | long | Returns a stream consisting of the remaining elements of this stream after discarding the first n elements of the stream. | Stream Pipelines |
peek() | Stream<T> | Consumer<T> | Returns a stream consisting of the elements of this stream, additionally performing the provided action on each element as elements are consumed from the resulting stream. | Stream Pipelines |
sorted() stateful-unbounded | Stream<T> | Compar | Returns a stream consisting of the elements of this stream, sorted according to natural order or provided Compar . | Stream Collectors |
Stateful and Stateless Operations Top
You may have noticed the stateful-bounded and stateful-unbounded entries in the Operation column. Intermediate stream operations can generally be considered as stateless e.g. the filter()
intermediate operator takes a value from the incoming stream and puts zero or one results into the outgoing stream, it doesn't need to know or retain any state, so this sort of operation can be thought of as stateless.
The distinct()
, limit()
, skip()
and sorted()
intermediate stream operations are stateful as they need to keep internal state to perform their tasks.
In the case of the limit()
and skip()
intermediate stream operations the state required is small as information of how many records to skip or limit the stream too can be held within a long
and so are bounded by this.
The distinct()
and sorted()
intermediate stream operations on the other hand need to know what happened to the stream previously to fulfill their tasks. This can lead to issues when dealing with large and infinite stre
Terminal Operations Top
There are quite a few terminal stream operations and you will use some a lot more than others. The following table lists all the terminal stream operations available:
Operation | Return Type | Type or Functional Interface | Description | Code Examples |
---|---|---|---|---|
allMatch() | boolean | Predicate | Returns whether all elements of this stream match the provided predicate. | Finding & Matching |
anyMatch() | boolean | Predicate | Returns whether any elements of this stream match the provided predicate. | Finding & Matching |
collect() | R | Collector | Performs a mutable reduction operation on the elements of this stream using a Collector. | Numeric Streams Reduction Operations |
count() | long | Returns the count of elements in this stream. | Introducing Streams Stream Pipelines Array Type Streams Other Stream Creation Reduction Operations | |
findAny() | Optional | Returns an Optional describing some element of the stream, or an empty Optional if the stream is empty. | Finding & Matching | |
findFirst() | Optional | Returns an Optional describing the first element of this stream, or an empty Optional if the stream is empty. | Finding & Matching | |
forEach() | void | Consumer | Performs an action for each element of this stream. | Stream Pipelines Array Type Streams |
min() | Optional | Compar | Returns the minimum element of this stream according to the provided Comparator . | Reduction Operations |
max() | Optional | Compar | Returns the maximum element of this stream according to the provided Comparator . | Reduction Operations |
noneMatch() | boolean | Predicate | Returns whether no elements of this stream match the provided predicate. | Finding & Matching |
reduce() | Optional | Binary | Performs a reduction on the elements of this stream, using an associative accumulation function, and returns an Optional describing the reduced value, if any. | Numeric Streams Reduction Operations |
toArray() | Object[] | Returns an array containing the elements of this stream.. | Numeric Streams |
Intermediate and Terminal Operation Differences Top
Now we have seen all the intermediate and terminal stream operations lets look at the differences between them.
- There can be zero or more intermediate operations but there must be one and only one terminal operation.
- Intermediate operations are lazily loaded and only executed after the terminal operation is eagerly loaded.
- The internal iteration of a stream is begun by the terminal operation not any intermediate operations.
- Intermediate operations produce a stream whereas a terminal operation produces a result.
Related Quiz
Streams Quiz 3 - Streams Operations Overview Quiz
Lesson 3 Complete
In this lesson we took a high level overview of stream operations including tables listing all the intermediate and terminal operations.
What's Next?
In the next lesson we look at creating streams from arrays.