DoubleStream peek() in Java with examples
DoubleStream peek() is a method in java.util.stream.DoubleStream. 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.
DoubleStream peek() is an intermediate operation. These operations are always lazy. Intermediate operations are invoked on a Stream instance and after they finish their processing, they give a Stream instance as output.
Syntax :
DoubleStream peek(DoubleConsumer action)
Parameters :
- DoubleStream : A sequence of primitive double-valued elements.
- DoubleConsumer : Represents an operation that accepts a single double-valued argument and returns no result.
Return Value : the function returns a parallel DoubleStream.
Note : This method exists mainly to support debugging.
Example 1 : Performing sum operation to find sum of elements of given DoubleStream.
// Java code for DoubleStream peek() // where the action performed is to get // sum of all elements. import java.util.*; import java.util.stream.DoubleStream; class GFG { // Driver code public static void main(String[] args) { // Creating a stream of doubles DoubleStream stream = DoubleStream.of(2.2, 3.3, 4.5, 6.7); // performing action sum on elements of // given range and storing the result in sum double sum = stream.peek(System.out::println).sum(); // Displaying the result of action performed System.out.println("sum is : " + sum); } } |
2.2 3.3 4.5 6.7 sum is : 16.7
Example 2 : Performing count operation on elements of given DoubleStream.
// Java code for DoubleStream peek() // where the action performed is to get // count of all elements in given range import java.util.*; import java.util.stream.DoubleStream; class GFG { // Driver code public static void main(String[] args) { // Creating a stream of doubles DoubleStream stream = DoubleStream.of(2.2, 3.3, 4.5, 6.7); // performing count operation on elements of // given DoubleStream 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); } } |
2.2 3.3 4.5 6.7 count : 4
Example 3 : Performing average operation on elements of given DoubleStream.
// Java code for DoubleStream peek() // where the action performed is to get // average of all elements. import java.util.*; import java.util.OptionalDouble; import java.util.stream.DoubleStream; class GFG { // Driver code public static void main(String[] args) { // Creating a stream of doubles DoubleStream stream = DoubleStream.of(2.2, 3.3, 4.5, 6.7); ; // performing action "average" on elements of // given DoubleStream 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"); } } } |
2.2 3.3 4.5 6.7 Average is : 4.175
Recommended Posts:
- DoubleStream max() in Java with examples
- DoubleStream min() in Java with examples
- Stream peek() in Java with examples
- IntStream peek() in Java with examples
- LongStream peek() in Java with examples
- DoubleStream noneMatch() in Java with examples
- DoubleStream count() in Java with examples
- DoubleStream distinct() in Java with examples
- DoubleStream average() in Java with Examples
- DoubleStream filter() in Java with examples
- DoubleStream builder() in Java with Examples
- DoubleStream boxed() in Java with Examples
- DoubleStream allMatch() in Java with examples
- DoubleStream anyMatch() in Java with examples
- DoubleStream empty() 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.

