Stream API
On this page 8 topics
Overview
Topics
- filter
- map
- flatMap
- reduce
- collectors
- grouping
Learn first
88 min of reading · 4 h of practice (estimate)
Learning outcomes
- Build pipelines with
filter,map,flatMap,sorted,distinct, andlimit. - Terminate streams with
collect,reduce,count,anyMatch, andfindFirst. - Group and aggregate with
Collectors.groupingBy,counting, andsummingDouble. - Decide when a plain loop is clearer than a stream.
Lessons
- 01Streams vs loops12 min read
- 02Intermediate operations10 min read
- 03Terminal operations and laziness12 min read
- 04flatMap10 min read
- 05Collectors and grouping11 min read
- 06When not to use a stream11 min read
- 07Demo: building the sales report pipeline10 min read
- 08Demo: rewriting an unreadable stream as a loop12 min read
Lab and exercises
Lab
BEG-036-LAB1 · Sales report
Outcomes: 1–4 · Time: ~2.5 h From a list of orders compute revenue by category, top customers, and average order value per month. Keep each pipeline short and readable; note any step you wrote as a loop and why. Acceptance: correct results, readable pipelines.
Exercises (assessment: six exercises)
| ID | Outcome | Task |
|---|---|---|
| BEG-036-EX01 | 1 | From a list of names, produce the distinct, upper-case names longer than 3 letters, sorted, first 5 only. |
| BEG-036-EX02 | 2 | Given integers, compute the sum with reduce, test whether any is negative, and count the evens. |
| BEG-036-EX03 | 1 | Given List<Order> where each has List<String> items, produce a flat list of distinct item names. |
| BEG-036-EX04 | 3 | Group people by city and count them; then group orders by category and sum the amounts. |
| BEG-036-EX05 | 2 | Find the first person older than 60 and print their name, or "none". |
| BEG-036-EX06 | 4 | Review: rewrite a 10-step stream chain with a side effect as a loop, and justify the change in two sentences. |
Show solutions
- EX01
names.stream().filter(n -> n.length() > 3).map(String::toUpperCase).distinct().sorted().limit(5).toList(); - EX02
reduce(0, Integer::sum);anyMatch(n -> n < 0);filter(n -> n % 2 == 0).count(). - EX03
orders.stream().flatMap(o -> o.getItems().stream()).distinct().toList(); - EX04
groupingBy(Person::getCity, counting());groupingBy(Order::getCategory, summingDouble(Order::getAmount)). - EX05
people.stream().filter(p -> p.getAge() > 60).findFirst()returns anOptional; for now use.map(Person::getName).orElse("none"). - EX06 A named-variable loop with the side effect inside; the answer says the loop is easier to read and debug and the side effect is explicit.
- Reusing a consumed stream is covered in demo L07 and quiz Q09.
Quiz
Outcomes: O1 intermediate operations · O2 terminal operations · O3 grouping and collectors · O4 streams vs loops.
A self-check for you: nothing is graded, saved or sent anywhere. Choose an answer, then check it. You can try again as often as you like.
Practical part
Six exercises BEG-036-EX01 to EX06 plus the lab. Pass: at least five exercises correct, EX06 justified, and all lab results correct.
Common mistakes
Side effects inside map; reusing a consumed stream; unreadable 15-step chains.
Assessment
6 exercises.
Career relevance
Everyday Java; analogous to SQL GROUP BY (BEG-050).