I'm trying to understand flatMap
: flatMap(x->stream.of(x) )
does not flat the stream and flatMap(x->x.stream())
works and gives the desired result.
Can someone explain the difference between two?
import java.util.*;
import java.util.stream.*;
class TestFlatMap{
public static void main(String args[]){
List<String> l1 = Arrays.asList("a","b");
List<String> l2 = Arrays.asList("c","d");
Stream.of(l1, l2).flatMap((x)->Stream.of(x)).forEach((x)->System.out.println(x));
Stream.of(l1, l2).flatMap((x)->x.stream()).forEach((x)->System.out.println(x));
}
}
Output :
[a, b]
[c, d]
a
b
c
d