×

java · level 3 · unit 3 · lesson 1

  1. meet it
  2. guess first
  3. your turn
  4. fix it
  5. nailed it

Tap a green word if you get lost.

meet it

A describes the result, not the loop.

Loops say how: make a list, go round, check, add. A stream says what. orders.stream() opens a stream of the items, filter keeps the ones you want, map changes each one, and collect(Collectors.toList()) gathers the results into a new list.

That chain is a pipeline. filter and map are intermediate operations: they set up work but do nothing yet. Only the at the end pulls items through. Without one, nothing runs at all.

The source list is never changed. A pipeline reads it and builds something new, which keeps the original safe for whoever uses it next.

import java.util.List;
import java.util.stream.Collectors;
public class Main {
    public static void main(String[] args) {
        List<String> orders = List.of("latte", "tea", "flat white", "mocha", "tea");
        List<String> labels = orders.stream()
            .filter(drink -> !drink.equals("tea"))
            .map(drink -> drink.toUpperCase())
            .collect(Collectors.toList());
        System.out.println(labels);
        System.out.println(orders);
    }
}

Put each step on its own line, starting with a dot, so the pipeline reads top to bottom like a recipe.