Course contentsShow
← Java
Lesson 1369 of 2,87031. Streams API: Creation and BasicsFree lesson

The Stream Pipeline: Source, Intermediate, Terminal

Learning the three-stage structure of stream processing: creating a source, applying transformations, producing a result.

The Stream Pipeline: Source, Intermediate, Terminal

You'll learn how streams process data through three distinct stages that work together like an assembly line.

The Core Idea

A stream pipeline is Java's way of expressing "what to do with data" rather than "how to do it step-by-step." Every stream operation follows a three-stage pattern:

  1. Source: Where the data comes from
  2. Intermediate operations: Transformations applied to the data (optional, can be chained)
  3. Terminal operation: Produces a final result and triggers execution

Think of it like a factory assembly line: raw materials enter (source), workers modify them at various stations (intermediate operations), and finally the finished product rolls off the line (terminal operation).

The Three Stages Explained

Source

This is your starting point—creating the stream from a collection, array, or generator. The data isn't processed yet; you're just setting up the pipeline.

Intermediate Operations

These transform or filter the stream, but here's the key: they're lazy. They don't execute immediately. You can chain multiple intermediate operations together, and they simply build up a recipe for what will happen.

Terminal Operation

This is the trigger. When you invoke a terminal operation, the entire pipeline springs to life. The stream processes elements, applies all intermediate operations, and produces your final result. After a terminal operation completes, the stream is consumed and cannot be reused.

The Assembly Line Analogy

Imagine ordering coffee: you specify the source (coffee beans), intermediate steps (grind, brew, add milk), and terminal action (pour into cup). Nothing happens until you say "make it"—that's your terminal operation.

Key Takeaway: Every stream pipeline has exactly one source and one terminal operation, with zero or more intermediate operations in between. The pipeline only executes when the terminal operation is invoked.