DoubleStream mapToObj() method in Java

Java 8Object Oriented ProgrammingProgramming

The mapToObj() method of the DoubleStream class returns an object-valued Stream consisting of the results of applying the given function to the elements of this stream.

The syntax is as follows −

<U> Stream<U> mapToObj(DoubleFunction<? extends U> mapper)

Here, the mapper is a stateless function to apply to each element and <U> is the element type of the new stream. The DoubleFunction signifies a function that accepts a double-valued argument and produces a result.

To work with DoubleStream class in Java, import the following page −

import java.util.stream.DoubleStream;

The following is an example to implement DoubleStream mapToObj() method −

Example

 Live Demo

import java.util.*;
import java.util.stream.DoubleStream;

public class Demo {
   public static void main(String[] args) {
      DoubleStream doubleStream = DoubleStream.of(15.2, 20.8, 45.5, 55.7, 88.3, 92.7);
      doubleStream.mapToObj(a ->{return a + a ;}).forEach(System.out::println);
   }
}

Output

30.4
41.6
91.0
111.4
176.6
185.4
raja
Updated on 30-Jul-2019 22:30:25

Advertisements