Stream flatMap(Function<? super T,? extends Stream<? extends R>> mapper)
returns a stream by replacing this stream with a mapped stream by applying the provided mapping function.
flatMap
has the following syntax.
<R> Stream<R> flatMap(Function<? super T,? extends Stream<? extends R>> mapper)
The following example shows how to use flatMap
.
import java.util.Arrays; import java.util.List; import java.util.stream.Stream; // w w w .j a v 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() .flatMap(n-> Stream.of(n) ) .forEach(System.out::println); } }
The code above generates the following result.