Repaso
EspaƱol
Java CoreIntermediateJV-19

What does this print?

Stream.of(1, 2, 3)
    .peek(x -> System.out.print("a" + x + " "))
    .map(x -> x * 2);
System.out.println("done");

Options

  1. a1 a2 a3 done
  2. done (Correct option)
  3. done a1 a2 a3
  4. Compile error

Answer

done

Why

Streams are lazy: the intermediate operations (peek, map, filter) only describe what to do. Nothing runs until a terminal operation (collect, forEach, count, toList...) is called.

Here there is no terminal operation, so the pipeline never runs and only "done" is printed.

Remember

No terminal operation = nothing executes.

Key terms

Practice this question in Repaso