This intermediate operation returns a stream consisting of the results of replacing each element of this stream with the contents of a mapped stream produced by applying the provided mapping function to each element. Each mapped stream is closed after its contents have been placed into this stream. If a mapped stream is null
an empty stream is used, instead.
package com.logicbig.example.doublestream;
import java.util.Arrays;
import java.util.stream.DoubleStream;
public class FlatMapExample {
public static void main(String... args) {
DoubleStream ds = DoubleStream.of(1.1, 2.4, 3.2, 4.3);
double[] doubles = ds.flatMap(value ->
DoubleStream.of(value * 0.5, value))
.toArray();
System.out.println(Arrays.toString(doubles));
}
}
Output
[0.55, 1.1, 1.2, 2.4, 1.6, 3.2, 2.15, 4.3]