mapToObj()
of the LongStream
interface is an operation that returns an object-valued stream. The stream has the results of applying the implementation of the LongFunction functional interface.
Note:
mapToObj()
is an intermediate operation. These operations are. Intermediate operations are run on a lazy They are delayed unless they become needed. Stream
instance. When they are done, they return aStream
instance as an output.
<U> Stream<U> mapToObj(LongFunction<? extends U> mapper);
LongFunction<? extends U> mapper
: The implementation of the LongFunction
interface.This method returns a new object-valued stream.
import java.util.stream.LongStream;import java.util.stream.Stream;class Main {public static void main(String[] args){// Create a LongStream using the rangeClosed methodLongStream stream = LongStream.rangeClosed(2, 7);// Using LongStream mapToObj(LongFunction mapper)// create a new stream// to store the bit count in the binary representation of the// elements in LongStreamStream<Integer> bitCountStream = stream.mapToObj(Long::bitCount);// Print the object-valued StreambitCountStream.forEach(System.out::println);}}