Java Streams - Stream flatMapToInt(Function mapper) example








Stream flatMapToInt(Function<? super T,? extends IntStream> mapper) returns an IntStream by replacing this stream with a mapped stream produced by applying the provided mapping function.

Syntax

flatMapToInt has the following syntax.

IntStream flatMapToInt(Function<? super T,? extends IntStream> mapper)

Example

The following example shows how to use flatMapToInt.

import java.util.Arrays;
import java.util.List;
import java.util.stream.IntStream;
//w w  w. java  2  s.c o  m
public class Main {
  public static void main(String[] args) {
    List<String> stringList = Arrays.asList("1.2","2.2","3","4","5");

    stringList.stream()
           .flatMapToInt(n-> IntStream.of(Integer.parseInt(n)) )
           .forEach(System.out::println);
  }
}