Intermediate Operations
A stream can take any number of intermediate operations following the creation of the stream. An intermediate operation is often a filter or mapping of some type, but there are other types as well. Every intermediate operation returns another stream; that way, you can chain any number of intermediate operations to your pipeline.
The order of intermediate operations is very important as the stream returned from an operation will only reference the remaining or required elements of the previous stream.
There are several different types of intermediate operations. The following is an explanation of each of them:
filter
: As the name suggests, this intermediate operation will return a subset of elements from the stream. It uses a predicate when applying the matching pattern, which is a functional interface that returns aBoolean
. The easiest and most common way to implement this is using a lambda function:Stream.of(1, 2, 3, 4, 5, 6) ...