Stream flatMapToDouble() in Java with examples

Stream flatMapToDouble(Function mapper) returns an DoubleStream 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. Stream flatMapToDouble(Function mapper) 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 flatMapToDouble(Function<? super T, ? extends DoubleStream> mapper)

Where, DoubleStream is a sequence of primitive
double-valued elements and T is the type 
of stream elements. mapper is a stateless function 
which is applied to each element and the function
returns the new stream.

Example 1 : flatMapToDouble() function with operation of parsing string to double.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code for Stream flatMapToDouble
// (Function mapper) to get an DoubleStream
// 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 a list of Strings
        List<String> list = Arrays.asList("1.5", "2.7", "3",
                                                "4", "5.6");
  
        // Using Stream flatMapToDouble(Function mapper)
        list.stream().flatMapToDouble(num 
        -> DoubleStream.of(Double.parseDouble(num)))
        .forEach(System.out::println);
    }
}

chevron_right


Output :

1.5
2.7
3.0
4.0
5.6

Example 2 : flatMapToDouble() function with operation of returning stream with length of strings.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code for Stream flatMapToDouble
// (Function mapper) to get an DoubleStream
// 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 a List of Strings
        List<String> list = Arrays.asList("Geeks", "GFG",
                                 "GeeksforGeeks", "gfg");
  
        // Using Stream flatMapToDouble(Function mapper)
        // to get length of all strings present in list
        list.stream().flatMapToDouble(str 
        -> DoubleStream.of(str.length()))
        .forEach(System.out::println);
    }
}

chevron_right


Output :

5.0
3.0
13.0
3.0


My Personal Notes arrow_drop_up

In love with a semicolon because sometimes i miss it so badly)

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.