Java Streams - IntStream flatMap(IntFunction mapper) example








IntStream flatMap(IntFunction<? extends IntStream> mapper) returns a stream by a mapped stream using mapping function.

Syntax

flatMap has the following syntax.

IntStream flatMap(IntFunction<? extends IntStream> mapper)

Example

The following example shows how to use flatMap.

import java.util.stream.IntStream;
/*  ww  w . j a v a 2  s.  c  om*/
public class Main {
  public static void main(String[] args) {
    IntStream i = IntStream.of(6,5,7,1, 2, 3, 3);
    IntStream i2 = i.flatMap(n -> IntStream.of(n*n));
    i2.forEach(System.out::println);  
    
  }
}

The code above generates the following result.