IntStream 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(IntToDoubleFunction mapper)

Here, the parameter mapper is the stateless function applied to each element.

Create an IntStream with some elements.

IntStream intStream = IntStream.of(5, 20, 25, 45, 60, 75, 85, 90);

Now, use the mapToDouble() method to return a DoubleStream.

DoubleStream doubleStream = intStream.mapToDouble(val -> (double) val);

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

Example

 Live Demo

import java.util.*;
import java.util.stream.IntStream;
import java.util.stream.DoubleStream;
public class Demo {
   public static void main(String[] args) {
      IntStream intStream = IntStream.of(5, 20, 25, 45, 60, 75, 85, 90);
      DoubleStream doubleStream = intStream.mapToDouble(val -> (double) val);
      doubleStream.forEach(System.out::println);
   }
}

Output

5.0
20.0
25.0
45.0
60.0
75.0
85.0
90.0
raja
Published on 27-Mar-2019 10:14:56
Advertisements