DoubleStream flatMap() method in Java


The flatMap() method of the DoubleStream class returns a stream with the results of replacing each element of this stream with the contents of a mapped stream formed by applying the provided mapping function to each element.

The syntax is as follows

DoubleStream flatMap(DoubleFunction<? extends DoubleStream> mapper)

Here, the parameter mapper is a stateless function to apply to each element which produces a DoubleStream of new values.

The DoubleFunction here is a function that accepts a double-valued argument and produces a result. To use the DoubleStreamclass in Java, import the following package

import java.util.stream.DoubleStream;

The following is an example to implement DoubleStream flatMap() method in Java

Example

 Live Demo

import java.util.*;
import java.util.stream.DoubleStream;
public class Demo {
   public static void main(String[] args) {
      DoubleStream doubleStream1 = DoubleStream.of(7.9, 20.6, 35.3, 68.5, 78.9, 99.4);
      DoubleStream doubleStream2 = doubleStream1.flatMap(a -> DoubleStream.of(a+a));
      doubleStream2.forEach(System.out::println);
   }
}

Output

15.8
41.2
70.6
137.0
157.8
198.8

Updated on: 30-Jul-2019

56 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements