Stream mapToDouble() in Java with examples

Stream mapToDouble (ToDoubleFunction mapper) returns a DoubleStream consisting of the results of applying the given function to the elements of this stream.

Stream mapToDouble (ToDoubleFunction 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.

Syntax :



DoubleStream mapToDouble(ToDoubleFunction<? super T> mapper)

Where, 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 : mapToDouble() with operation of selecting elements satisfying given function.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code for Stream mapToDouble
// (ToDoubleFunction mapper) to get a
// DoubleStream by applying the given function
// to the elements of this stream.
import java.util.*;
  
class GFG {
  
    // Driver code
    public static void main(String[] args)
    {
  
        // Creating a list of Strings
        List<String> list = Arrays.asList("10", "6.548", "9.12",
                                                    "11", "15");
  
        // Using Stream mapToDouble(ToDoubleFunction mapper)
        // and displaying the corresponding DoubleStream
        list.stream().mapToDouble(num -> Double.parseDouble(num))
                    .filter(num -> (num * num) * 2 == 450)
                    .forEach(System.out::println);
    }
}

chevron_right


Output :

15.0

Example 2 : mapToDouble() with operation of returning a stream with square of string length.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code for Stream mapToDouble
// (ToDoubleFunction mapper) to get a
// DoubleStream by applying the given function
// to the elements of this stream.
import java.util.*;
  
class GFG {
  
    // Driver code
    public static void main(String[] args)
    {
        // Creating a list of Strings
        List<String> list = Arrays.asList("CSE", "JAVA", "gfg",
                                                    "C++", "C");
  
        // Using Stream mapToDouble(ToDoubleFunction mapper)
        // and displaying the corresponding DoubleStream
        // which contains square of length of each element in
        // given Stream
        list.stream().mapToDouble(str -> str.length() * str.length())
                     .forEach(System.out::println);
    }
}

chevron_right


Output :

9.0
16.0
9.0
9.0
1.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.