In Java, the mapToObj()
method of the IntStream
interface is used to return an object-valued stream. This stream has the results obtained by applying the implementation of the IntFunction
functional interface.
Note:
mapToObj()
is an intermediate operation. This operation is performed on a stream instance and results in a stream instance.
<U> Stream<U> mapToObj(IntFunction<? extends U> mapper);
IntFunction<? extends U> mapper
: This is the implementation of the IntFunction
interface.This method returns a new object-valued stream.
import java.util.stream.Stream;import java.util.stream.IntStream;class Main {public static void main(String[] args){IntStream stream = IntStream.rangeClosed(2, 7);Stream<Integer> integerStream = stream.mapToObj(Integer::bitCount);integerStream.forEach(System.out::println);}}
rangeClosed()
method to create an IntStream
.mapToObj(IntFunction mapper)
of the IntStream
interface to create a new stream called integerStream
, which stores the number of set bits of each integer in the stream.integerStream
.