Close

Java 8 Streams - Stream.mapToDouble Examples

Java 8 Streams Java Java API 


Interface:

java.util.stream.Stream

java.lang.AutoCloseableAutoCloseablejava.util.stream.BaseStreamBaseStreamjava.util.stream.StreamStreamLogicBig

Method:

DoubleStream mapToDouble(ToDoubleFunction<? super T> mapper)

This intermediate operation returns a DoubleStream consisting of the results of applying the given mapper function to the elements of this stream.


Examples


package com.logicbig.example.stream;

import java.util.stream.DoubleStream;
import java.util.stream.Stream;

public class MapToDoubleExample {

public static void main(String... args) {
String[] s = {"1.1", "2.4", "3.7", "4.33"};
Stream<String> stringStream = Stream.of(s);
DoubleStream doubleStream = stringStream.mapToDouble(Double::parseDouble);
double sum = doubleStream.peek(System.out::println)
.sum();
System.out.println("sum: " + sum);
}
}

Output

1.1
2.4
3.7
4.33
sum: 11.530000000000001




See Also