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.
Syntax :
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.
// Java code for IntStream peek() // where the action performed is to get // sum of all elements in given range import java.util.*; import java.util.stream.IntStream; class GFG { // Driver code public static void main(String[] args) { // Creating a stream of integers IntStream stream = IntStream.range(2, 10); // performing action sum on elements of // given range and storing the result in sum int sum = stream.peek(System.out::println).sum(); // Displaying the result of action performed System.out.println("sum is : " + sum); } } |
Output :
2 3 4 5 6 7 8 9 sum is : 44
Example 2 : Performing count operation on a stream of given range.
// Java code for IntStream peek() // where the action performed is to get // count of all elements in given range import java.util.*; import java.util.stream.IntStream; class GFG { // Driver code public static void main(String[] args) { // Creating a stream of integers IntStream stream = IntStream.range(2, 10); // performing action count on elements of // given range and storing the result in Count long Count = stream.peek(System.out::println).count(); // Displaying the result of action performed System.out.println("count : " + Count); } } |
Output :
2 3 4 5 6 7 8 9 count : 8
Example 3 : Performing Average operation on a stream of given range.
// Java code for IntStream peek() // where the action performed is to get // average of all elements in given range import java.util.*; import java.util.OptionalDouble; import java.util.stream.IntStream; class GFG { // Driver code public static void main(String[] args) { // Creating a stream of integers IntStream stream = IntStream.range(2, 10); // performing action average on elements of // given range and storing the result in avg OptionalDouble avg = stream.peek(System.out::println) .average(); // If a value is present, isPresent() // will return true, else -1 is displayed. if(avg.isPresent()) { System.out.println("Average is : " + avg.getAsDouble()); } else { System.out.println("-1"); } } } |
Output :
2 3 4 5 6 7 8 9 Average is : 5.5
Recommended Posts:
- LongStream peek() in Java with examples
- DoubleStream peek() in Java with examples
- Stream peek() in Java with examples
- BlockingDeque peek() method in Java with examples
- IntStream max() in Java with examples
- IntStream min() in Java with Examples
- IntStream count() in Java with examples
- IntStream distinct() in Java with examples
- IntStream average() in Java with Examples
- IntStream toArray() in Java with Examples
- IntStream noneMatch() in Java with examples
- IntStream empty() in Java with examples
- IntStream allMatch() in Java with examples
- IntStream anyMatch() in Java with examples
- IntStream filter() in Java with examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



