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
- a1 a2 a3 done
- done (Correct option)
- done a1 a2 a3
- 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.