LongStream mapToDouble() method in Java

Java 8Object Oriented ProgrammingProgramming

The mapToDouble() method returns a DoubleStream consisting of the results of applying the given function to the elements of this stream.

The syntax is as follows:

DoubleStream mapToDouble(LongToDoubleFunction mapper)

The parameter mapper is a stateless function to apply to each element.

To use the LongStream class in Java, import the following package:

import java.util.stream.LongStream;

The following is an example to implement LongStream mapToDouble() method in Java

Example

import java.util.stream.LongStream;
import java.util.stream.DoubleStream;
public class Demo {
   public static void main(String[] args) {
      LongStream longStream = LongStream.of(5000L, 12000L, 15000L, 20000L, 25000L);
      DoubleStream s = longStream.mapToDouble(a → (double)a);
      System.out.println("Elements of DoubleStream...");
      s.forEach(System.out::println);
   }
}

output

Elements of DoubleStream...
5000.0
12000.0
15000.0
20000.0
25000.0
raja
Published on 28-Mar-2019 15:45:27
Advertisements