DoubleStream flatMap(DoubleFunction mapper) in Java
DoubleStream flatMap(DoubleFunction mapper) 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. This is an intermediate operation. These operations are always lazy. Intermediate operations are invoked on a Stream instance and after they finish their processing, they give a Stream instance as output.
Note : 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.
Syntax :
DoubleStream flatMap(DoubleFunction<? extends DoubleStream> mapper)
Parameters :
- DoubleStream : A sequence of primitive double-valued elements.
- DoubleFunction : A function that accepts an double-valued argument and produces a result.
- mapper : A stateless function which is applied to each element and the function returns the new stream.
Return Value : DoubleStream flatMap(DoubleFunction mapper) returns a stream by a mapped stream using mapping function.
Example 1 : Using DoubleStream flatMap() to get the cube of elements of DoubleStream.
// Java code for DoubleStream flatMap // (DoubleFunction mapper) to get a stream // consisting of the results of replacing // each element of this stream with the // contents of a mapped stream import java.util.*; import java.util.stream.DoubleStream; class GFG { // Driver code public static void main(String[] args) { // Creating an DoubleStream DoubleStream stream1 = DoubleStream.of(4.2, 5.3, 6.6, 7.0); // Using DoubleStream flatMap() DoubleStream stream2 = stream1.flatMap(num -> DoubleStream.of(num * num * num)); // Displaying the resulting DoubleStream stream2.forEach(System.out::println); } } |
74.08800000000001 148.87699999999998 287.496 343.0
Example 2 :Using DoubleStream flatMap() to multiply every element of DoubleStream by 0.7
// Java code for DoubleStream flatMap // (DoubleFunction mapper) to get a stream // consisting of the results of replacing // each element of this stream with the // contents of a mapped stream import java.util.*; import java.util.stream.DoubleStream; class GFG { // Driver code public static void main(String[] args) { // Creating an DoubleStream DoubleStream stream1 = DoubleStream.of(5.2, 6.4, 8.1, 10.0); // Using DoubleStream flatMap() DoubleStream stream2 = stream1.flatMap(num -> DoubleStream.of(num * 0.7)); // Displaying the resulting IntStream stream2.forEach(System.out::println); } } |
3.6399999999999997 4.4799999999999995 5.669999999999999 7.0
Recommended Posts:
- DoubleStream map(DoubleUnaryOperator mapper) in Java
- LongStream map(LongUnaryOperator mapper) in Java
- IntStream map(IntUnaryOperator mapper) in Java
- LongStream flatMap(LongFunction mapper) in Java
- IntStream flatMap(IntFunction mapper) in Java
- Java | Collectors averagingLong (ToLongFunction mapper) with Examples
- DoubleStream of() in Java
- DoubleStream sum() in Java
- DoubleStream sequential() in Java
- DoubleStream skip() in Java
- DoubleStream iterator() in Java
- DoubleStream summaryStatistics() in Java
- DoubleStream parallel() in Java
- DoubleStream sorted() in Java
- DoubleStream concat() in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

