Intermediate operations
We have already seen how a Stream
object that represents a source and emits elements can be created. As we have mentioned already, the operations (methods) provided by the Stream
interface can be divided into three groups:
- The methods that create a
Stream
object based on a source. - Intermediate operations that accept a function and produce a
Stream
object that emits the same, or modified, values. - Terminal operations that complete the stream processing, close it, and produce the result.
In this section, we will review the intermediate operations that, in turn, can be grouped by their functionalities.
Filtering
This group includes operations that remove duplicates, skip some of the elements, and limit the number of processed elements, only selecting those that are needed:
Stream<T> distinct()
: Compares stream elements using theObject.equals(Object)
method, and skips the duplicates.Stream<T> skip(long n)
: Ignores the provided number of stream elements that are...