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.
LongStream flatMap(LongFunction<? extends LongStream> mapper)
- 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.
64 125 216 343
Example 2 : Using LongStream flatMap() to get the count of set bits in binary representation of elements of LongStream.
3 1 3 3
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.