DoubleStream mapToInt() method in Java

Java 8Object Oriented ProgrammingProgramming

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
raja
Published on 28-Mar-2019 12:42:52
Advertisements