Java Streams - DoubleStream flatMap(DoubleFunction mapper) example








DoubleStream flatMap(DoubleFunction<? extends DoubleStream> mapper) returns a stream by mapping stream using mapping function.

Syntax

flatMap has the following syntax.

DoubleStream flatMap(DoubleFunction<? extends DoubleStream> mapper)

Example

The following example shows how to use flatMap.

import java.util.stream.DoubleStream;

public class Main {
  public static void main(String[] args) {
    DoubleStream b = DoubleStream.of(1.1,2.2,3.3,4.4,5.5);
    b.flatMap(n -> DoubleStream.of(n*n))
     .forEach(System.out::println);
  }
}

The code above generates the following result.