IntStream flatMap(IntFunction mapper) in Java

IntStream flatMap(IntFunction mapper) returns a stream consisting of the results of replacing each element of this stream with the contents of a mapped stream produced by applying the provided mapping function to each element. This is an 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.

Note : Each mapped stream is closed after its contents have been placed into this stream. If a mapped stream is null, an empty stream is used, instead.

Syntax :



IntStream flatMap(IntFunction<? extends IntStream> mapper)

Parameters :

  1. IntStream : A sequence of primitive int-valued elements.
  2. IntFunction : A function that accepts an int-valued argument and produces a result.
  3. mapper : A stateless function which is applied to each element and the function returns the new stream.

Return Value : IntStream flatMap(IntFunction mapper) returns a stream by a mapped stream using mapping function.

Example 1 : Using IntStream flatMap() to get the cube of elements of IntStream.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code for IntStream flatMap
// (IntFunction mapper) to get a stream
// consisting of the results of replacing
// each element of this stream with the
// contents of a mapped stream
import java.util.*;
import java.util.stream.IntStream;
  
class GFG {
  
    // Driver code
    public static void main(String[] args)
    {
        // Creating an IntStream
        IntStream stream1 = IntStream.of(4, 5, 6, 7);
  
        // Using IntStream flatMap()
        IntStream stream2 = stream1.flatMap(num
                  -> IntStream.of(num * num * num));
  
        // Displaying the resulting IntStream
        stream2.forEach(System.out::println);
    }
}

chevron_right


Output :

64
125
216
343

Example 2 :Using IntStream flatMap() to get the count of set bits in binary representation of elements of IntStream.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code for IntStream flatMap
// (IntFunction mapper) to get a stream
// consisting of the results of replacing
// each element of this stream with the
// contents of a mapped stream
import java.util.*;
import java.util.stream.IntStream;
  
class GFG {
  
    // Driver code
    public static void main(String[] args)
    {
        // Creating an IntStream
        IntStream stream1 = IntStream.of(49, 64, 81, 100);
  
        // Using IntStream flatMap()
        IntStream stream2 = stream1.flatMap(num
               -> IntStream.of(Integer.bitCount(num)));
  
        // Displaying the resulting IntStream
        stream2.forEach(System.out::println);
    }
}

chevron_right


Output :

3
1
3
3


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.