I have been writing about some important methods from Java SE 8 e.g. map(), flatMap() etc from quite some time and today I'll share my experience about another useful method peek() from java.utill.stream.Stream class. The peek() method of Stream class can be a very useful to debug and understand streams in Java 8. You can use the peek() method to see the elements as they flow from one step to another e.g. when you use the filter() method for filtering, you can actually see how filtering is working e.g. lazy evaluation as well as which elements are filtered. the peek() method returns a stream consisting of the elements of this stream and performs the action requested by the client. The peek() method expect a Consumer instance to perform a non-interfering action on the elements of this stream, usually printing them using forEach() method.
Read more »