Java Streams - Stream flatMapToDouble(Function mapper) example








Stream flatMapToDouble(Function<? super T,? extends DoubleStream> mapper) returns an DoubleStream by replacing each element with the contents of a mapped stream produced by applying the mapping function.

Syntax

flatMapToDouble has the following syntax.

DoubleStream flatMapToDouble(Function<? super T,? extends DoubleStream> mapper)

Example

The following example shows how to use flatMapToDouble.

import java.util.Arrays;
import java.util.List;
import java.util.stream.DoubleStream;
//from ww w. ja v a2 s .  co  m
public class Main {
  public static void main(String[] args) {
    List<String> stringList = Arrays.asList("1.2","2.2","3","4","5");

    stringList.stream()
           .flatMapToDouble(n-> DoubleStream.of(Double.parseDouble(n)) )
           .forEach(System.out::println);
  }
}

The code above generates the following result.