Composing Stream Pipelines

Stream operations can be chained together to compose a stream pipeline in which stream components are specified in the following order:

  • An operation on a data source for building the initial stream
  • Zero or more intermediate operations to transform one stream into another
  • A single mandatory terminal operation in order to execute the pipeline and produce some result or side effect

Composition into a pipeline is possible because stream creation and intermediate operations return a stream, allowing method calls to be chained as we have seen in Figure 16.1a.

The chain of method calls in Figure 16.1a forms the stream pipeline in Figure 16.1b, showing the components of the pipeline. A stream pipeline is analogous to an assembly line, where each operation depends on the result of the previous operation as parts are assembled. In a pipeline, an intermediate operation consumes elements made available by its input stream to produce elements that form its output stream. The terminal operation produces the final result from its input stream. Creating a stream pipeline can be regarded as a fusion of stream operations, where only a single pass of the elements is necessary to process the stream.

Stream operations are typically customized by behavior parameterization that is specified by functional interfaces and implemented by lambda expressions. That is why understanding built-in functional interfaces and writing method references (or their equivalent lambda expressions) is essential. In Figure 16.1a, the Predicate argument of the filter() operation implements the behavior of the filter() operation.

A stream pipeline formulates a query on the elements of a stream created from a data source. It expresses what should be done to the stream elements, and not how it should be done—analogous to a database query. One important advantage of composing stream pipelines is that the compiler can freely optimize the operations— for example, for parallel execution—as long as the same result is guaranteed.

Leave a Reply

Your email address will not be published. Required fields are marked *