LongStream asDoubleStream() method in Java

Java 8Object Oriented ProgrammingProgramming

The asDoubleStream() method of the LongStream class in Java returns a DoubleStream consisting of the elements of this stream, converted to double.

The syntax is as follows.

DoubleStream asDoubleStream()

Here, DoubleStream is a sequence of primitive double-valued elements. To use the LongStream class in Java, import the following package.

import java.util.stream.LongStream;

Create LongStream and add elements.

LongStream longStream = LongStream.of(2000L, 35000L, 45000L);

Now, convert it to double and return using asDoubleStream() method.

DoubleStream s = longStream.asDoubleStream();

The following is an example to implement LongStream asDoubleStream() method.

Example

 Live Demo

import java.util.stream.LongStream;
import java.util.stream.DoubleStream;
public class Demo {
   public static void main(String[] args) {
      LongStream longStream = LongStream.of(2000L, 35000L, 45000L);
      DoubleStream s = longStream.asDoubleStream();
      System.out.println("DoubleStream...");
      s.forEach(System.out::println);
   }
}

Output

DoubleStream...
2000.0
35000.0
45000.0
raja
Published on 27-Mar-2019 10:56:21
Advertisements