DoubleStream peek() method in Java

Java 8Object Oriented ProgrammingProgramming

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
raja
Published on 28-Mar-2019 07:46:00
Advertisements