🎯 What you will learn
The Streams API lets you express data transformations as declarative pipelines rather than imperative loops. A stream is not a data structure — it is a view of a source that processes elements lazily on demand. By the end of this lesson you will understand:
- Stream pipeline: source → intermediate (lazy) → terminal (eager)
filter,map,sorted,distinct,limit- Terminal operations:
collect,count,sum,reduce,forEach mapToDoubleandsummaryStatistics()Collectors.groupingByand downstream collectorsflatMapto flatten nested collectionstakeWhileanddropWhile(Java 9+)
🧾 Real-world scenario
TaxSetu handles payroll and tax filings for hundreds of companies. Meera needs to: filter engineering staff, total salaries, find high earners, group invoices by department and sum them, flatten employee skill sets for a competency report, and use takeWhile to slice ordered score data. Streams let her express all of this without a single mutable accumulator variable.
🌊 Stream basics — filter, map, collect
A stream pipeline has three parts: a source (list.stream()), zero or more intermediate operations (each returns a new Stream), and exactly one terminal operation that triggers execution.
Laziness in action: Calling staff.stream().filter(...).map(...) does nothing until a terminal operation (collect, forEach, count) is appended. This allows short-circuiting — findFirst() stops as soon as one match is found.
📊 Aggregations — reduce, groupingBy, summaryStatistics
reduce is a general fold; mapToDouble(...).sum() is a convenient shortcut for numeric sums. Collectors.groupingBy partitions the stream into a Map<K, List<V>>; downstream collectors like summingDouble replace the default list accumulation.
🔬 Advanced — flatMap, distinct, takeWhile
flatMap is the 1-to-many operation: each element produces a Stream, and all those streams are merged into one. Use it to flatten nested collections. takeWhile (Java 9+) stops processing as soon as the predicate fails — useful for ordered data.
📋 Stream pipeline stages
| Stage | Operations | Returns |
|---|---|---|
| Source | stream(), of(), generate() | Stream<T> |
| Intermediate (lazy) | filter, map, flatMap, sorted, distinct, limit, takeWhile | Stream<T> |
| Terminal (eager) | collect, forEach, count, sum, reduce, findFirst, anyMatch | Result |
🧩 Mini challenge
Using the Employee record from StreamBasics, use Collectors.groupingBy with a downstream Collectors.averagingDouble to compute the average salary per department. Print each department and its average, sorted descending by average salary.
Use Collectors.partitioningBy to split invoices into those above ₹1,50,000 (large) and those at or below (small). Print the count of each partition and the total amount for large invoices.
Given the skillSets list from StreamAdvanced, use flatMap + groupingBy(counting()) to find the top 3 most common skills. Use limit(3) after sorting by count descending.
🎤 Interview Q&A
Intermediate operations (filter, map, sorted) are lazy — they return a new Stream and don't execute until a terminal operation is reached. Terminal operations (collect, count, forEach) are eager — they trigger the entire pipeline to run and produce a result. This laziness allows short-circuiting: findFirst() after filter() stops as soon as one match is found.
collect(Collectors.toList()) do?It is a terminal operation that triggers the pipeline and accumulates elements into a new ArrayList. Equivalent to iterating the stream and calling list.add() on each element. Java 16+ added stream.toList() as a shorter alternative that returns an unmodifiable list.
groupingBy work under the hood?Collectors.groupingBy(classifier) partitions the stream into a Map<K, List<V>> where each key is the result of applying classifier to an element. Internally it performs a mutable reduction — similar to iterating and calling map.computeIfAbsent(key, k -> new ArrayList<>()).add(element). Downstream collectors (like summingDouble) replace the default toList collector for each group.
parallelStream() and when should you avoid it?Use parallelStream() when: the data set is large (thousands+ elements), the per-element work is CPU-bound and stateless, and the source supports efficient splitting (ArrayList yes, LinkedList no). Avoid it when: elements must be processed in order, lambdas have shared mutable state, or the overhead of forking threads outweighs the work — common for small lists or I/O-bound operations.
reduce and collect in the Streams API.reduce is a functional fold — it combines elements immutably using an associative binary operator: (acc, e) -> acc + e. It's ideal for computing a single value from numeric data. collect is a mutable reduction — it accumulates elements into a mutable container like a List or Map using a Collector. collect is more efficient when building collections because it avoids creating intermediate objects on every combination step.
📝 Quiz — 5 questions
filter, map, sorted are:flatMap vs map — when do you use flatMap?summaryStatistics() on a DoubleStream gives you:takeWhile(pred) — what does it do?Next: Exceptions and try-with-resources →
⚠️ Common mistakes
stream.filter(...).count() then stream.map(...) on the same variable throws IllegalStateException. Create a fresh stream from the source each time.parallelStream() splits work across threads; output order is non-deterministic. Use .forEachOrdered() or collect to a list if order matters.map or filter are bad practice and break in parallel streams. Only use forEach as the terminal operation for intentional side-effects.staff.stream().filter(...) alone does nothing. You must add a terminal operation (collect, forEach, count) to trigger execution.✅ Key takeaways
- Streams are lazy pipelines: intermediate ops stack up, terminal ops trigger execution.
- Each Stream can be consumed once only — recreate from the source.
maptransforms 1-to-1;flatMapflattens 1-to-many.groupingBypartitions into Map;summaryStatistics()gives count/min/max/avg/sum.takeWhile/dropWhile(Java 9+) add short-circuit ordered filtering.
From Streams to production Spring Boot
Join the Best AI Programs — three 5-month courses, fully online, with placement assistance. Week 1 is free.
Explore our AI-era courses →