Understanding stream pipelines
A stream in Java is a sequence of data that can be processed by operations. Streams are not another way to organize data, such as using an array or Collection
, because streams do not hold data. Streams are all about efficiently processing data that is flowing by.
Let’s look at the stream pipeline.
Stream pipeline
A stream pipeline is a set of operations that run on a stream to produce a result. At a minimum, a stream pipeline consists of a source, zero or more intermediate operations, and a terminal operation, in that order. A pipeline is similar to an assembly line in a factory. Let’s look at an example.
Assembly line analogy
Let’s assume we have a task of sharpening and stamping pencils that are currently sitting in a box (which contains 100 pencils). Stamping them means marking the pencil type on the pencil, such as 2B, 2H, and so forth. The pencils must be sharpened, stamped, and finally packed away, in that order...