Java 8 Streams peek api

The Javadoc mentions the following:

Intermediate operations return a new stream. They are always lazy; executing an intermediate operation such as filter() does not actually perform any filtering, but instead creates a new stream that, when traversed, contains the elements of the initial stream that match the given predicate. Traversal of the pipeline source does not begin until the terminal operation of the pipeline is executed.

peek being an intermediate operation does nothing. On applying a terminal operation like foreach, the results do get printed out as seen.

Leave a Comment