Close

Java 8 Streams - IntStream.flatMap Examples

Java 8 Streams Java Java API 


Interface:

java.util.stream.IntStream

java.lang.AutoCloseableAutoCloseablejava.util.stream.BaseStreamBaseStreamjava.util.stream.IntStreamIntStreamLogicBig

Method:

IntStream flatMap(IntFunction<? extends IntStream> 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.intstream;

import java.util.Arrays;
import java.util.stream.IntStream;

public class FlatMapExample {
//IntStream flatMap(IntFunction<? extends IntStream> mapper)

public static void main(String... args) {
int[] ints = {2, 3, 5, 7};
IntStream stream = Arrays.stream(ints);
IntStream stream2 = stream.flatMap(FlatMapExample::multipleOf);
stream2.forEach(System.out::println);
}

private static IntStream multipleOf(int i) {
return IntStream.iterate(i, j -> j + i)
.limit(5);
}
}

Output

2
4
6
8
10
3
6
9
12
15
5
10
15
20
25
7
14
21
28
35




See Also