Chapter 5 · SimulationStream Pipeline (Stream API)? ← Chapter 5Java codeEvents (last 4)Output CloseStream Pipeline (Stream API)Goal: see how a Stream pipeline processes elements. A pipeline consists of a source (numbers.stream()), zero or more intermediate operations (filter, map, sorted, limit) and a single terminal operation (collect, forEach, findFirst).How to use itPick a ready-made example, or set the source list (at most 8 numbers, 0–99), the intermediate operations and the terminal operation yourself.Step ▶ / ◀ Back (keyboard → / ←), Play, Reset. In the lane in the middle each box is an operation; the box that is working right now is highlighted. At the bottom you see the last four events and the program's output.With Test yourself on, you make a prediction at some steps (the first map result, whether the source will stop, the result).What to look forLazy evaluation: intermediate operations only describe the pipeline. Until a terminal operation is called, no element is processed (terminal operation: none).Element by element: the elements are not first all filtered and then all transformed; each element is pulled from the source and travels through the pipeline on its own until the end (or until it is filtered out). With forEach you can see the output being produced element by element.sorted is a stateful operation: it cannot sort before all elements have arrived, so it collects them and sends them downstream in order once the source is finished.Short-circuiting: after limit(k) has let k elements through, or findFirst() has found the first element, no new element is pulled from the source. If a sorted sits in between, the source is still read to the end.The order of events and the outputs were checked with javac --release 8 + java by adding logging to the lambdas and peek calls of the same pipelines.Related slides: Chapter 5 · Collections · The Stream API.