🎯 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
  • mapToDouble and summaryStatistics()
  • Collectors.groupingBy and downstream collectors
  • flatMap to flatten nested collections
  • takeWhile and dropWhile (Java 9+)
💼 Interview reality: "Explain intermediate vs terminal operations" and "when do streams execute?" are asked in virtually every Java 8+ backend round. Understanding laziness is the key insight interviewers probe for.

🧾 Real-world scenario

🧾
Real-world scenario · TaxSetu, Bangalore
Meera builds a payroll and invoice analytics pipeline

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.

javac StreamBasics.java && java StreamBasics
StreamBasics.javaLesson 17 · filter, map, sorted, count, sum
verified output
=== TaxSetu — Employee Stream === Engineering dept (3): Meera ₹125,000 Arjun ₹98,000 Karthik ₹140,000 Earning > ₹90k: Arjun Karthik Meera Ravi Total payroll : ₹615,000.00 Finance headcount: 2

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.

javac StreamAgg.java && java StreamAgg
StreamAgg.javaLesson 17 · reduce, groupingBy, summaryStatistics
verified output
=== TaxSetu — Invoice Aggregations === Total invoiced : ₹1,010,000.00 Min invoice : ₹95,000.00 Max invoice : ₹280,000.00 Average : ₹168,333.33 By department: IT ₹630,000.00 Finance ₹205,000.00 Audit ₹175,000.00 Largest invoice: TCS ₹280,000.00

🔬 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.

javac StreamAdvanced.java && java StreamAdvanced
StreamAdvanced.javaLesson 17 · flatMap, distinct, groupingBy counting, takeWhile
verified output
=== TaxSetu — Advanced Stream Ops === All skills (flat) : 12 entries Unique sorted : [Docker, Java, Kafka, Python, React, SQL, Spring] Skill frequency: Java 3 SQL 3 Spring 2 Docker 1 Kafka 1 React 1 Python 1 Scores >= 70 (takeWhile): [95, 88, 72]

📋 Stream pipeline stages

StageOperationsReturns
Sourcestream(), of(), generate()Stream<T>
Intermediate (lazy)filter, map, flatMap, sorted, distinct, limit, takeWhileStream<T>
Terminal (eager)collect, forEach, count, sum, reduce, findFirst, anyMatchResult

🧩 Mini challenge

Three stream exercises
Task 1 — Department salary report

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.

Task 2 — Invoice partitioning

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.

Task 3 — Top-N skill finder

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

EasyWhat is the difference between intermediate and terminal Stream operations?
+

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.

EasyWhat does 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.

MediumHow does 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.

MediumWhen should you use 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.

HardExplain the difference between 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

Question 1 of 5
filter, map, sorted are:
Question 2 of 5
Can you reuse a Stream after calling a terminal operation?
Question 3 of 5
flatMap vs map — when do you use flatMap?
Question 4 of 5
summaryStatistics() on a DoubleStream gives you:
Question 5 of 5
takeWhile(pred) — what does it do?
0
/ 5
Quiz complete!

Next: Exceptions and try-with-resources →

⚠️ Common mistakes

Reusing a Stream
A Stream can only be consumed once. Calling stream.filter(...).count() then stream.map(...) on the same variable throws IllegalStateException. Create a fresh stream from the source each time.
Expecting ordered output from parallel streams
parallelStream() splits work across threads; output order is non-deterministic. Use .forEachOrdered() or collect to a list if order matters.
Using forEach for side-effects inside a collect pipeline
Side-effects (printing, mutating external state) inside map or filter are bad practice and break in parallel streams. Only use forEach as the terminal operation for intentional side-effects.
Not collecting — leaving an un-terminated pipeline
Intermediate operations are lazy. 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.
  • map transforms 1-to-1; flatMap flattens 1-to-many.
  • groupingBy partitions into Map; summaryStatistics() gives count/min/max/avg/sum.
  • takeWhile/dropWhile (Java 9+) add short-circuit ordered filtering.
Africa & India's Best AI Academy
🤖 AI-first 🎓 Placement Assistance

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 →