What does peek() do in Java streams?

peek() is an intermediate Stream operation that lets you observe each element as it flows through the pipeline, typically for debugging, without altering the stream's contents.

Key Points

Example

Adding .peek(System.out::println) between filter() and collect() lets you print each element right after it passes the filter, which is handy for verifying an intermediate step while debugging a pipeline.

List<Integer> peekedAtNumbers = numbers.stream()
        .peek(System.out::println)
        .collect(Collectors.toList());

Interview Tip

A concise interview answer is

peek() lets me observe elements as they flow through a stream pipeline, mainly for debugging with something like System.out::println, without changing the elements themselves. It only fires once a terminal operation runs, since streams are lazily evaluated, and it shouldn't be used to drive real application logic.

Last updated: 11 November 2026

Previous question: How do you group elements using Java streams?

Next question: How do you limit a stream in Java?

More Stream API Coding Questions - Level I interview questions

All Stream API Coding Questions - Level I questions