Java Streams
Map / Filter / Reduce
Transform collections with declarative pipelines: select, transform, and aggregate data in a single fluent chain.
What Is a Stream?
A Stream is a sequence of elements that supports functional-style operations to process data. Unlike a collection, a stream does not store data: it pipelines elements from a source through a chain of transformations.
Imperative vs Declarative
Traditional loops are imperative: you tell Java how to iterate. Streams are declarative: you describe what you want, and the library handles iteration.
The Three Core Operations
The Pipeline Pattern
Every stream pipeline has three stages: a source, zero or more intermediate operations, and exactly one terminal operation.
map(): Transform Every Element
map() applies a function to each element and returns a new stream of the results. The output stream always has the same number of elements as the input.
filter(): Select Matching Elements
filter() takes a Predicate (a function returning boolean) and keeps only elements for which the predicate returns true.
reduce(): Combine Into One
reduce() is the terminal operation that folds all elements into a single result by repeatedly applying a binary operator.
Intermediate vs Terminal
| Intermediate | Terminal | |
|---|---|---|
| Returns | Stream<\( T \)> | Value / Collection |
| Lazy? | Yes: deferred until terminal | No: triggers execution |
| Chainable? | Yes: returns a stream | No: ends the pipeline |
| Examples | filter, map, sorted, distinct | collect, reduce, forEach, count |
How to Build a Pipeline
Example 1: Active User Emails
Given a list of User objects, extract the email addresses of all active users and collect them into a list.
Example 2: Sum of Squares
Compute the sum of squares of all positive numbers in a list, combining all three operations in one pipeline.
Example 3: Longest Word
Find the longest word in a list. This example uses filter to clean data, map to normalize, and a terminal operation to select the max.
Method References: Cleaner Lambdas
When a lambda simply calls an existing method, you can replace it with a method reference for brevity.
| Lambda | Method Reference |
|---|---|
| s → s.toUpperCase() | String::toUpperCase |
| s → s.length() | String::length |
| n → Math.abs(n) | Math::abs |
| x → System.out.println(x) | System.out::println |
Common Mistakes
Quick Reference Table
| Operation | Type | Returns | Purpose |
|---|---|---|---|
| filter() | Intermediate | Stream<\( T \)> | Select elements |
| map() | Intermediate | Stream<\( R \)> | Transform elements |
| sorted() | Intermediate | Stream<\( T \)> | Order elements |
| distinct() | Intermediate | Stream<\( T \)> | Remove duplicates |
| reduce() | Terminal | \( T \) or Optional<\( T \)> | Combine into one |
| collect() | Terminal | Collection | Gather results |
| forEach() | Terminal | void | Side effect per item |
| count() | Terminal | long | Count elements |
Question 1 of 5
Which stream operation transforms each element into a new value without changing the number of elements?
Question 2 of 5
What happens if you call two terminal operations on the same stream object?
Question 3 of 5
What is the result of the following stream pipeline?
Question 4 of 5
What is the identity value in stream.reduce(0, Integer::sum)?
Question 5 of 5
Which of the following is an intermediate operation?