Java Streams - Stream flatMapToLong(Function mapper) example








Stream flatMapToLong(Function<? super T,? extends LongStream> mapper) returns an LongStream by replacing each element with a mapped stream produced by applying the provided mapping function.

Syntax

flatMapToLong has the following syntax.

LongStream flatMapToLong(Function<? super T,? extends LongStream> mapper)

Example

The following example shows how to use flatMapToLong.

import java.util.Arrays;
import java.util.List;
import java.util.stream.LongStream;
/* ww w .  jav a 2s.  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()
           .flatMapToLong(n-> LongStream.of(Long.parseLong(n)) )
           .forEach(System.out::println);
  }
}