mapToDouble()
of the Stream
interface returns a DoubleStream
. The DoubleStream
contains the results of the the ToDoubleFunction function’s provided implementation on the stream’s items.
DoubleStream mapToInt(ToDoubleFunction<? super T> mapper)
ToDoubleFunction<? super T> mapper
: The mapper function to apply.This method returns a new stream:
import java.util.Arrays;import java.util.List;import java.util.function.ToDoubleFunction;public class Main{public static void main(String[] args) {// define the list of doublesList<Double> doubleList = Arrays.asList(10.4, 24.5, 54.1, 8.0,23.2);// Define the implementation of ToDoubleFunction interfaceToDoubleFunction<Double> multiplyByTwo = e -> e * 2;// apply the multiplyByTwo using mapToDouble function and print each valuedoubleList.stream().mapToDouble(multiplyByTwo).forEach(System.out::println);}}
ToDoubleFunction
interface called multiplyByTwo
. Here, we multiply the input double value by 2
.multiplyByTwo
using the mapToDouble
function and print the result.