IntStream flatMap(IntFunction mapper) in Java Last Updated : 06 Dec, 2018 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 : IntStream : A sequence of primitive int-valued elements. IntFunction : A function that accepts an int-valued argument and produces a result. 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. Java // 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); } } Output : 64 125 216 343 Example 2 :Using IntStream flatMap() to get the count of set bits in binary representation of elements of IntStream. Java // 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); } } Output : 3 1 3 3 Comment More infoAdvertise with us Next Article IntStream mapToObj() in Java S Sahil_Bansall Follow Improve Article Tags : Java Java - util package Java-Functions java-stream java-intstream +1 More Practice Tags : Java Similar Reads 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, t 2 min read IntStream mapToDouble() in Java IntStream mapToDouble() returns a DoubleStream consisting of the results of applying the given function to the elements of this stream. Note : IntStream mapToDouble() is a intermediate operation. These operations are always lazy. Intermediate operations are invoked on a Stream instance and after the 2 min read IntStream map(IntUnaryOperator mapper) in Java IntStream map(IntUnaryOperator mapper) returns a stream consisting of the results of applying the given function to the elements of this stream. This is an intermediate operation. These operations are always lazy. Intermediate operations are invoked on a Stream instance and after they finish their p 2 min read IntFunction Interface in Java with Examples The IntFunction Interface is a part of the java.util.function package which has been introduced since Java 8, to implement functional programming in Java. It represents a function which takes in an int-valued argument and produces a result of type R. This functional interface takes in only one gener 1 min read Map Interface in Java In Java, the Map Interface is part of the java.util package and represents a mapping between a key and a value. The Java Map interface is not a subtype of the Collections interface. So, it behaves differently from the rest of the collection types.Key Features:No Duplicates in Keys: Keys should be un 11 min read Map.Entry interface in Java with example Map.Entry interface in Java provides certain methods to access the entry in the Map. By gaining access to the entry of the Map we can easily manipulate them. Map.Entry is a generic and is defined in the java.util package. Declaration : Interface Map.Entry k -> Key V -> Value Methods: equals (O 4 min read Like