Sub topic 3
Anatomy of a Stream Pipeline
A stream pipeline is a chain of steps that data flows through. Every pipeline has the same three parts. Once you know the parts, you can read any stream code.
The Three Parts
- Source. A source is where the elements come from. Most often it is a collection, such as a
List, and you call.stream()on it. - Intermediate operations. An intermediate operation is a step that takes a stream and returns a new stream. Examples are
filterandmap. A pipeline can have zero, one, or many of them. - Terminal operation. A terminal operation is the last step. It produces a final result, such as a list or a number, and it ends the stream. Examples are
countandcollect.
A Worked Example
This program keeps the even numbers, multiplies each by 10, and gathers the results into a new list.
import java.util.List;
import java.util.stream.Collectors;
public class PipelineExample {
public static void main(String[] args) {
List<Integer> numbers = List.of(4, 7, 10, 3, 8);
List<Integer> result = numbers.stream() // source
.filter(n -> n % 2 == 0) // intermediate
.map(n -> n * 10) // intermediate
.collect(Collectors.toList()); // terminal
System.out.println(result); // [40, 100, 80]
System.out.println(numbers); // [4, 7, 10, 3, 8]
}
}Read it from top to bottom:
numbers.stream()creates a stream from the list.filter(n -> n % 2 == 0)lets only even numbers pass: 4, 10, and 8.map(n -> n * 10)changes each number: 40, 100, and 80.collect(Collectors.toList())puts the results into a new list.
The original numbers list did not change. A stream reads from its source but does not modify it.
How to Tell the Parts Apart
Look at what each method returns.
| Kind | Returns | Examples | How many per pipeline |
|---|---|---|---|
| Source | A `Stream` | `list.stream()`, `Stream.of(...)` | Exactly one |
| Intermediate | A `Stream` | `filter`, `map`, `sorted`, `distinct` | Zero or more |
| Terminal | Something that is not a stream | `collect`, `count`, `forEach`, `findFirst` | Exactly one |
If a method returns a Stream, you can keep chaining. If it returns something else, the pipeline is over.
Writing Pipelines Clearly
- Put each operation on its own line, starting with a dot. This makes each step easy to see.
- Read the pipeline aloud as a sentence: "From numbers, keep the evens, times ten, collect to a list."
- Keep each lambda short. If a step gets long, move the logic into a named method and use a method reference.
Exercise
In the pipeline words.stream().map(String::trim).filter(w -> !w.isEmpty()).count(), name the source, each intermediate operation, and the terminal operation. What type does the whole expression return?
Show answer
The source is words.stream(). The intermediate operations are map(String::trim) and filter(w -> !w.isEmpty()). The terminal operation is count(). The expression returns a long, the number of non-empty trimmed words.
Exercise
A classmate writes numbers.stream().filter(n -> n > 5).map(n -> n * 2); and expects it to print or store something. Why does nothing happen?
Show answer
The pipeline has no terminal operation. filter and map are intermediate operations that only describe the work. Without a terminal operation such as collect or count, the stream never runs and produces no result.