Close

Java 8 Streams - LongStream.flatMap Examples

Java 8 Streams Java Java API 


Interface:

java.util.stream.LongStream

java.lang.AutoCloseableAutoCloseablejava.util.stream.BaseStreamBaseStreamjava.util.stream.LongStreamLongStreamLogicBig

Method:

LongStream flatMap(LongFunction<? extends LongStream> mapper)

This intermediate operation 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. 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.

Examples


package com.logicbig.example.longstream;

import java.util.Arrays;
import java.util.stream.LongStream;

public class FlatMapExample {

public static void main(String... args) {
long[] ints = {10, 20, 30, 40};
LongStream stream = Arrays.stream(ints);
LongStream stream2 = stream.flatMap(FlatMapExample::multipleOf);
stream2.forEach(System.out::println);
}

private static LongStream multipleOf(long l) {
return LongStream.iterate(l, j -> j + l)
.limit(5);
}
}

Output

10
20
30
40
50
20
40
60
80
100
30
60
90
120
150
40
80
120
160
200




See Also