DoubleStream mapToInt() method in Java


The mapToInt() function of the DoubleStream class returns an IntStream consisting of the results of applying the given function to the elements of this stream.

The syntax is as follows

IntStream mapToInt(DoubleToIntFunction mapper)

Here, mapper is a stateless function to apply to each element. The DoubleToIntFunction is a function that accepts a double-valued argument and produces an int-valued 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 mapToInt() method in Java

Example

 Live Demo

import java.util.stream.IntStream;
import java.util.stream.DoubleStream;
public class Demo {
   public static void main(String[] args) {
      DoubleStream doubleStream = DoubleStream.of(45.8, 78.9, 67.8, 59.1);
      IntStream intStream = doubleStream.mapToInt(a -> (int)a);
      intStream.forEach(System.out::println);
   }
}

Output

45
78
67
59

Updated on: 30-Jul-2019

87 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements