Skip to content
Ameen Abozaid
BEG-036

Stream API

  • 8 Lessons
  • 88 min of reading
  • 4 h practice (estimate)
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

  1. Build pipelines with filter, map, flatMap, sorted, distinct, and limit.
  2. Terminate streams with collect, reduce, count, anyMatch, and findFirst.
  3. Group and aggregate with Collectors.groupingBy, counting, and summingDouble.
  4. Decide when a plain loop is clearer than a stream.

Lessons

  1. 01Streams vs loops12 min read
  2. 02Intermediate operations10 min read
  3. 03Terminal operations and laziness12 min read
  4. 04flatMap10 min read
  5. 05Collectors and grouping11 min read
  6. 06When not to use a stream11 min read
  7. 07Demo: building the sales report pipeline10 min read
  8. 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)

IDOutcomeTask
BEG-036-EX011From a list of names, produce the distinct, upper-case names longer than 3 letters, sorted, first 5 only.
BEG-036-EX022Given integers, compute the sum with reduce, test whether any is negative, and count the evens.
BEG-036-EX031Given List<Order> where each has List<String> items, produce a flat list of distinct item names.
BEG-036-EX043Group people by city and count them; then group orders by category and sum the amounts.
BEG-036-EX052Find the first person older than 60 and print their name, or "none".
BEG-036-EX064Review: 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 an Optional; 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.

  1. 1Which keeps only matching items?O1
  2. 2Which transforms each item into another value?O1
  3. 3flatMap is used to:O1
  4. 4A pipeline with no terminal operation:O2
  5. 5findFirst() returns:O2
  6. 6Which combines all items into one value?O2
  7. 7Collectors.groupingBy(Person::getCity) returns:O3
  8. 8To count items per group, use as downstream:O3
  9. 9Using a stream a second time after a terminal operation:O1
  10. 10A loop is usually clearer when:O4

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

Type to search levels, phases, modules and lessons.