IntStream peek() in Java with examples
IntStream peek() is a method in java.util.stream.IntStream. 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.
IntStream peek(IntConsumer action) Where, IntStream is a sequence of primitive int-valued elements and the function returns a parallel IntStream and IntConsumer represents an operation that accepts a single int-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.