Streams are the key abstraction in Java 8 for processing collections of values and specifying what you want to have done, leaving the scheduling of operations to the implementation. Furthermore, streams can leverage multi-core architectures without you having to write a single line of multithread code, and simplify the description of aggregate computations, exponing opportunities for optimisation. Streams basically allow us to write collections-processing code at a higher level of abstraction. You can think about stream as a pipeline where we are processing data, what we call the source, and we put it into zero or more intermediate operations; each operation takes an input stream and generates an output stream. That way we can take the output of one intermediate operation and feed it to the following intermedite operation. Once we have done all the intermediate processing that we want, we need to terminate that stream and we do that with a terminal operation; terminal operation takes an input stream and does not generate a stream as an output, but what it produces is either explicit results, such as a value, collections and so on, or a side effect, that is, for example a simple message. This pipeline is depicted in the following image:
Streams in Java 8
· 8 min read