Java 8 Streams peek api

Solution 1:

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.

Solution 2:

The documentation for peek says

Returns a stream consisting of the elements of this stream, additionally performing the provided action on each element as elements are consumed from the resulting stream. This is an intermediate operation.

You therefore have to do something with the resulting stream for System.out.println to do anything.