IntStream flatMap(IntFunction mapper) example

Description

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;
//w ww .j  a  v a 2 s .c o  m
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.





















Home »
  Java Streams »
    Reference »




Collectors
DoubleStream
DoubleStream.Builder
IntStream
IntStream.Builder
LongStream
LongStream.Builder
Stream
Stream.Builder