DoubleStream peek() method in Java


The syntax is as follows

DoubleStream peek(DoubleConsumer action)

Here, DoubleConsumer is an operation that accepts a single double-valued argument and returns no result.

To use the DoubleStream class in Java, import the following package

import java.util.stream.DoubleStream;

The following is an example to implement DoubleStream peek() method in Java

Example

 Live Demo

import java.util.*;
import java.util.stream.DoubleStream;
public class Demo {
   public static void main(String[] args) {
      DoubleStream doubleStream = DoubleStream.of(28.7, 35.6, 48.3, 69.8, 75.8, 80.5, 90.8);
      System.out.println("Elements in the stream...");
      long num = doubleStream.peek(System.out::println).count();
      System.out.println("Number of elements in the stream = " + num);
   }
}

Output

Elements in the stream...
28.7
35.6
48.3
69.8
75.8
80.5
90.8
Number of elements in the stream = 7

Updated on: 30-Jul-2019

116 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements