Java Stream flat map

Introduction

Stream flatMap() does an one-to-many map on a stream.

Then flatten the stream of streams to a stream.


import java.util.stream.Stream;

public class Main {
  public static void main(String[] args) {
    Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
    .flatMap(n -> Stream.of(n, n+1))
    .forEach(System.out::println);

  }/*w  ww. jav a2 s.com*/
}



PreviousNext

Related