IntStream mapToObj() in Java
Last Updated :
06 Dec, 2018
IntStream mapToObj() returns an object-valued Stream consisting of the results of applying the given function.
Note : IntStream mapToObj() is a intermediate operation. These operations are always lazy. Intermediate operations are invoked on a Stream instance and after they finish their processing, they give a Stream instance as output.
Syntax :
<U> Stream<U> mapToObj(IntFunction<? extends U> mapper)
Parameters :
- U : The element type of the new stream.
- Stream : A sequence of elements supporting sequential and parallel aggregate operations.
- IntFunction : Represents a function that accepts an int-valued argument and produces a result.
- mapper : A stateless function to apply to each element.
Return Value : The function returns an object-valued Stream consisting of the results of applying the given function.
Example 1 :
import java.util.*;
import java.util.stream.Stream;
import java.util.stream.IntStream;
class GFG {
public static void main(String[] args)
{
IntStream stream = IntStream.range( 3 , 8 );
Stream<String> stream1 = stream.mapToObj(num
-> Integer.toBinaryString(num));
stream1.forEach(System.out::println);
}
}
|
Output :
11
100
101
110
111
Example 2 :
import java.util.*;
import java.math.BigInteger;
import java.util.stream.Stream;
import java.util.stream.IntStream;
class GFG {
public static void main(String[] args)
{
IntStream stream = IntStream.of( 3 , 5 , 7 , 9 , 11 );
Stream<BigInteger> stream1 = stream
.mapToObj(BigInteger::valueOf);
stream1.forEach(num -> System.out.println(num.add(BigInteger.TEN)));
}
}
|
Output :
13
15
17
19
21
Share your thoughts in the comments
Please Login to comment...