The mapToObj() is a method of DoubleStream interface used to return an object-valued stream containing the results of applying the implementation of the DoubleFunction functional interface.
Note:
mapToObj()is an intermediate operation. These operations are lazy. Intermediate operations run on aStreaminstance, and when they’re done, they return aStreaminstance as an output.
<U> Stream<U> mapToObj(DoubleFunction<? extends U> mapper);
DoubleFunction<? extends U> mapper: The implementation of the DoubleFunction interface.This method returns a new object-valued stream.
import java.util.stream.DoubleStream;import java.util.stream.Stream;class Main {public static void main(String[] args){// Create a DoubleStream using the of methodDoubleStream stream = DoubleStream.of(2.1, 4.5, 2.5);// Using DoubleStream mapToObj(DoubleFunction mapper)// create a new stream// to store the hexadecimal representation of the// elements in DoubleStreamStream<String> hexStream = stream.mapToObj(Double::toHexString);// Print an object-valued StreamhexStream.forEach(System.out::println);}}
DoubleStream using the of() method.mapToObj(DoubleFunction mapper) of the DoubleStream interface we create a new stream called hexStream to store the hexadecimal representation of the elements in DoubleStream.hexStream.