Loading published curriculum…

03 · Practical Java / LESSON 8 OF 102

Records, lambdas, and streams

Transform collections without mixing unrelated side effects.

Before this lesson: Exceptions and resource management

Understand the topic

A record is a data carrier with generated accessors, equality, and hashCode. Components are final but referenced objects are not deeply immutable. Lambdas implement functional interfaces.

A stream has a source, intermediate operations, and a terminal operation. Intermediate operations are lazy. Avoid mutating shared external state in pipelines; parallelStream is not automatically faster.

EXAMPLE · JAVA

Main.java

import java.util.List;
public class Main {
    record Task(String title, boolean done) {}
    public static void main(String[] args) {
        var tasks = List.of(new Task("Java", true), new Task("SQL", false));
        var pending = tasks.stream().filter(t -> !t.done()).map(Task::title).toList();
        System.out.println(pending);
    }
}

What to expect

[SQL]

YOUR TURN

Put it into practice

  1. 01Sort pending titles.
  2. 02Count completed tasks.
  3. 03Rewrite the pipeline as a loop.

Pass a quiz or complete a lab to check today’s streak. Each lesson counts once per day, including when you review it later.

UNDER THE HOOD

Build the mental model

A stream is a description of a computation over a source, not another container holding the elements.

  1. 01filter keeps matching elements; map transforms them; flatMap combines nested streams. A terminal operation drives evaluation.
  2. 02A stream is consumed once. Reuse the source collection to start another pipeline instead of saving an already consumed stream.
  3. 03Records provide value equality for their components. A record containing a mutable list can still observe that list changing; copy collections when you need an immutable snapshot.

A mistake worth avoiding

Side effects in filter or map make results harder to reason about, especially in parallel.

DEBUGGING ROOM

Something broke. Where do you look?

A pipeline does nothing even though a map lambda contains a print.

Before revealing the diagnosis, write down your first investigation step.

CHECK YOUR UNDERSTANDING

Which operation triggers a normal lazy stream pipeline?

Choose one answer

BUILD IT YOURSELF

Create a task report

  1. 1.Filter incomplete tasks.
  2. 2.Group titles by category.
  3. 3.Produce a sorted, immutable result snapshot.

Acceptance checklist

Lab completion is your self-report. Knowledge checks do not certify project quality.

RETRIEVE IT FROM MEMORY

Why should transformations avoid mutating external variables?

Revisit this lesson in three days after a successful check or lab.

Ready for the next step?

Explain filter, map, terminal operations, and shallow immutability.

Reading is separate from passing a knowledge check or completing a lab.

Go deeper · official references