LongStream peek() is a method in java.util.stream.LongStream. The function 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.
LongStream peek(LongConsumer action) Where, LongStream is a sequence of primitive long-valued elements and the function returns a parallel LongStream and LongConsumer represents an operation that accepts a single long-valued argument.
Example 1 : Performing sum on a stream of given range.
2 3 4 5 6 7 8 9 sum is : 44
Example 2 : Performing count operation on a stream of given range.
2 3 4 5 6 7 8 9 count : 8
Example 3 : Performing average operation on a stream of given range.
2 3 4 5 6 7 8 9 Average is : 5.5
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.