LongStream mapToDouble() method in Java


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

Updated on: 30-Jul-2019

91 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements