Examining intermediate operations
As we know, a stream pipeline consists of a source, followed by zero or more intermediate operations, followed by a terminal operation. While the terminal operation is mandatory, intermediate operations are not. That said, intermediate operations are where pipelines get their real power as they transform the stream data as it flows by. Unlike terminal operations, intermediate operations produce a stream as a result. Let us start with filter()
, which is taken from IntermediateOperations.java on the repo:
filter(Predicate)
The filter()
operation returns a stream containing the elements matching the given predicate. Figure 16.1 presents a code example (from IntermediateOperations.java
on the repo):
Figure 16.1 - The filter(Predicate) intermediate operation in code
In this figure, the countries whose names are longer than 5 characters are output.
distinct()
The distinct()
operation returns a stream with duplicate...