IntStream mapToObj() in Java

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 :



  1. U : The element type of the new stream.
  2. Stream : A sequence of elements supporting sequential and parallel aggregate operations.
  3. IntFunction : Represents a function that accepts an int-valued argument and produces a result.
  4. 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 :

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code for IntStream mapToObj
// (IntFunction mapper)
import java.util.*;
import java.util.stream.Stream;
import java.util.stream.IntStream;
  
class GFG {
  
    // Driver code
    public static void main(String[] args)
    {
        // Creating an IntStream
        IntStream stream = IntStream.range(3, 8);
  
        // Creating a Stream of Strings
        // Using IntStream mapToObj(IntFunction mapper)
        // to store binary representation of
        // elements in IntStream
        Stream<String> stream1 = stream.mapToObj(num
                                                 -> Integer.toBinaryString(num));
  
        // Displaying an object-valued Stream
        // consisting of the results of
        // applying the given function.
        stream1.forEach(System.out::println);
    }
}

chevron_right


Output :

11
100
101
110
111

Example 2 :

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code for IntStream mapToObj
// (IntFunction mapper)
import java.util.*;
  
import java.math.BigInteger;
import java.util.stream.Stream;
import java.util.stream.IntStream;
  
class GFG {
  
    // Driver code
    public static void main(String[] args)
    {
        // Creating an IntStream
        IntStream stream = IntStream.of(3, 5, 7, 9, 11);
  
        // Creating a Stream
        // Using IntStream mapToObj(IntFunction mapper)
        Stream<BigInteger> stream1 = stream
                                         .mapToObj(BigInteger::valueOf);
  
        // Displaying an object-valued Stream
        // consisting of the results of
        // applying the given function.
        stream1.forEach(num -> System.out.println(num.add(BigInteger.TEN)));
    }
}

chevron_right


Output :

13
15
17
19
21


My Personal Notes arrow_drop_up

In love with a semicolon because sometimes i miss it so badly)

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.