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.
// 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); } } |
Output :
15.0
Example 2 : mapToDouble() with operation of returning a stream with square of string length.
// 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); } } |
Output :
9.0 16.0 9.0 9.0 1.0
Recommended Posts:
- IntStream mapToDouble() in Java
- LongStream mapToDouble() in Java
- Stream.of(T t) in Java with examples
- Stream map() in Java with examples
- Stream flatMapToInt() in Java with examples
- Stream flatMap() in Java with examples
- Stream flatMapToDouble() in Java with examples
- Stream flatMapToLong() in Java with examples
- Stream findFirst() in Java with examples
- Stream allMatch() in Java with examples
- Stream anyMatch() in Java with examples
- Stream filter() in Java with examples
- Java Stream findAny() with examples
- Stream empty() in Java with Examples
- Stream.max() method in Java with Examples
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.



