DoubleStream mapToLong() method in Java


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

The syntax is as follows

LongStream mapToLong(DoubleToLongFunction mapper)

Here, the parameter mapper is a stateless function to apply to each element. The DoubleToLongFunction here is a function that accepts a double-valued argument and produces a long-valued result.

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

import java.util.stream.DoubleStream;

Create a DoubleStream and add some elements

DoubleStream doubleStream = DoubleStream.of(30.5, 45.8, 89.3);

Now, use the LongStream and set a condition for the stream elements

LongStream longStream = doubleStream.mapToLong(a -> (long)a);

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

Example

 Live Demo

import java.util.stream.LongStream;
import java.util.stream.DoubleStream;
public class Demo {
   public static void main(String[] args) {
      DoubleStream doubleStream = DoubleStream.of(30.5, 45.8, 89.3);
      LongStream longStream = doubleStream.mapToLong(a -> (long)a);
      longStream.forEach(System.out::println);
   }
}

Output

30
45
89

Updated on: 30-Jul-2019

74 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements