Stream mapToDouble() in Java with examples Last Updated : 06 Dec, 2018 Comments Improve Suggest changes Like Article Like Report 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 // 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 // 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 Comment More infoAdvertise with us Next Article Stream mapToDouble() in Java with examples S Sahil_Bansall Follow Improve Article Tags : Misc Java Technical Scripter Java - util package Java-Functions java-stream Java-Stream interface +3 More Practice Tags : JavaMisc Similar Reads Stream mapToLong() in Java with examples Stream mapToLong(ToLongFunction mapper) returns a LongStream consisting of the results of applying the given function to the elements of this stream. Stream mapToLong(ToLongFunction mapper) is an intermediate operation. These operations are always lazy. Intermediate operations are invoked on a Strea 2 min read Stream mapToInt() in Java with examples Stream mapToInt(ToIntFunction mapper) returns an IntStream consisting of the results of applying the given function to the elements of this stream. Stream mapToInt(ToIntFunction mapper) is an intermediate operation. These operations are always lazy. Intermediate operations are invoked on a Stream in 2 min read OptionalDouble stream() method in Java with examples The stream() method help us to get double value contain by OptionalDouble as DoubleStream.If a value is present, method returns a sequential DoubleStream containing only that value, otherwise returns an empty DoubleStream. Syntax: public DoubleStream stream() Parameters: This method accepts nothing. 1 min read Stream peek() Method in Java with Examples In Java, Stream provides an powerful alternative to process data where here we will be discussing one of the very frequently used methods named peek() which being a consumer action basically returns a stream consisting of the elements of this stream, additionally performing the provided action on ea 2 min read Optional stream() method in Java with examples The stream() method of java.util.Optional class in Java is used to get the sequential stream of the only value present in this Optional instance. If there is no value present in this Optional instance, then this method returns returns an empty Stream. Syntax: public Stream<T> stream() Paramete 2 min read Stream count() method in Java with examples long count() returns the count of elements in the stream. This is a special case of a reduction (A reduction operation takes a sequence of input elements and combines them into a single summary result by repeated application of a combining operation). This is a terminal operation i.e, it may travers 2 min read Stream forEach() method in Java with examples Stream forEach(Consumer action) performs an action for each element of the stream. Stream forEach(Consumer action) is a terminal operation i.e, it may traverse the stream to produce a result or a side-effect. Syntax : void forEach(Consumer<? super T> action) Where, Consumer is a functional int 2 min read OptionalInt stream() method in Java with examples The stream() method help us to get value contain by OptionalInt as IntStream. If a value is present, method returns a sequential IntStream containing only that value, otherwise returns an empty IntStream. Syntax: public IntStream stream() Parameters: This method accepts nothing. Return value: This m 1 min read OptionalLong stream() method in Java with examples The stream() method help us to get Long value contain by OptionalLong as LongStream.If a value is present, method returns a sequential LongStream containing only that value, otherwise returns an empty LongStream. Syntax: public LongStream stream() Parameters: This method accepts nothing. Return valu 1 min read Stream generate() method in Java with examples Stream generate(Supplier<T> s) returns an infinite sequential unordered stream where each element is generated by the provided Supplier. This is suitable for generating constant streams, streams of random elements, etc. Syntax : static <T> Stream<T> generate(Supplier<T> s) Wh 1 min read Like