Repaso
Español
Java CoreIntermediateJV-18

What does this print?

List<String> names = List.of("Ana", "Bob", "Alicia", "Carl");
List<String> r = names.stream()
    .filter(n -> n.startsWith("A"))
    .map(String::toUpperCase)
    .toList();
System.out.println(r);

Options

  1. [Ana, Alicia]
  2. [ANA, ALICIA] (Correct option)
  3. [ANA, BOB, ALICIA, CARL]
  4. [A, A]

Answer

[ANA, ALICIA]

Why

A Stream is a pipeline for processing a collection: source, then intermediate steps, then a final (terminal) step.

filter keeps names starting with "A" (Ana, Alicia). map(String::toUpperCase) converts each to upper case. toList() collects the result.

The original list is not modified. n -> n.startsWith("A") is a lambda: a short anonymous function.

Remember

Stream = source, intermediate ops (filter/map), terminal op (toList/collect).

Key terms

Practice this question in Repaso