Summary
In this chapter, we explored the fundamentals of streams and stream terminal operations. Streams (along with lambda expressions) enable a style of programming known as functional-style programming, where you state what you want to solve rather than how to solve it (imperative style). Functional-style programming tends to be easier to read because, with imperative programming, the details of how to solve the problem can get mixed up in the implementation.
We discussed stream pipelines using the analogy of an assembly line. A stream pipeline consists of a data source, zero or more intermediate operations, and a terminal operation, in that order. Streams are lazily evaluated, which means that data is only provided as and when needed. This is possible because the JVM has an overall view of the pipeline, as nothing happens until the terminal operation is executed.
Stream sources can vary from arrays (Arrays.stream(arrayToUse)
), collections (collectionToUse.stream()
), and...