LongStream flatMap(LongFunction mapper) in Java Last Updated : 06 Dec, 2018 Summarize Comments Improve Suggest changes Share Like Article Like Report LongStream flatMap(LongFunction 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 : LongStream flatMap(LongFunction<? extends LongStream> mapper) Parameters : LongStream : A sequence of primitive long-valued elements. LongFunction : A function that accepts an long-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 : LongStream flatMap(LongFunction mapper) returns a stream by a mapped stream using mapping function. Example 1 : Using LongStream flatMap() to get the cube of elements of LongStream. Java // Java code for LongStream flatMap // (LongFunction 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.LongStream; class GFG { // Driver code public static void main(String[] args) { // Creating an LongStream LongStream stream1 = LongStream.of(4L, 5L, 6L, 7L); // Using LongStream flatMap() LongStream stream2 = stream1.flatMap(num -> LongStream.of(num * num * num)); // Displaying the resulting LongStream stream2.forEach(System.out::println); } } Output : 64 125 216 343 Example 2 : Using LongStream flatMap() to get the count of set bits in binary representation of elements of LongStream. Java // Java code for LongStream flatMap // (LongFunction 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.LongStream; class GFG { // Driver code public static void main(String[] args) { // Creating an LongStream LongStream stream1 = LongStream.of(49L, 64L, 81L, 100L); // Using LongStream flatMap() LongStream stream2 = stream1.flatMap(num -> LongStream.of(Long.bitCount(num))); // Displaying the resulting LongStream stream2.forEach(System.out::println); } } Output : 3 1 3 3 Comment More infoAdvertise with us Next Article Stream flatMap() in Java with examples S Sahil_Bansall Follow Improve Article Tags : Java Practice Tags : Java Similar Reads 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. Inte 2 min read Stream mapToLong() in Java with examples Stream mapToLong(ToLongFunction mapper) returns a LongStream consisting of the results of applying the given function to the elements of this stream. Stream mapToLong(ToLongFunction mapper) is an intermediate operation. These operations are always lazy. Intermediate operations are invoked on a Strea 2 min read LongFunction Interface in Java with Examples The LongFunction 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 a long-valued argument and produces a result of type R. This functional interface takes in only one gene 1 min read ToLongFunction Interface in Java with Examples The ToLongFunction 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 argument of type T and produces a long-valued result. This functional interface takes in only one g 1 min read Stream flatMap() in Java with examples Stream flatMap(Function 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. Stream flatMap(Function mapper) is an intermediate operation. These operations 5 min read LongAccumulator longValue() method in Java with Examples The java.LongAccumulator.longValue() is an inbuilt method in java that is equivalent to the get() method that is this method just returns the current value. Syntax: public long longValue() Parameters: This method does not accepts any parameter. Return Value: The method returns the current value. Bel 1 min read Like