map和flatmap的區別
map只是一維 1對1 的映射
而flatmap可以將一個2維的集合映射成一個一維,相當於他映射的深度比map深了一層 ,
所以名稱上就把map加了個flat 叫flatmap
flatMap的用法和含義住要通過一個案例來講解,
案例:對給定單詞列表 ["Hello","World"],你想返回列表["H","e","l","o","W","r","d"]
第一種方式
1
2
3
4
5
6
|
String[] words =
new
String[]{
"Hello"
,
"World"
};
List<String[]> a = Arrays.stream(words)
.map(word -> word.split(
""
))
.distinct()
.collect(toList());
a.forEach(System.out::print);
|
代碼輸出為:[Ljava.lang.String;@12edcd21[Ljava.lang.String;@34c45dca
(返回一個包含兩個String[]的list)
這個實現方式是由問題的,傳遞給map方法的lambda為每個單詞生成了一個String[](String列表)。因此,map返回的流實際上是Stream<String[]> 類型的。你真正想要的是用Stream<String>來表示一個字符串。
下方圖是上方代碼stream的運行流程
第二種方式:flatMap(對流扁平化處理)
1
2
3
4
5
6
7
|
String[] words =
new
String[]{
"Hello"
,
"World"
};
List<String> a = Arrays.stream(words)
.map(word -> word.split(
""
))
.flatMap(Arrays::stream)
.distinct()
.collect(toList());
a.forEach(System.out::print);
|
或者:
1
2
|
String[] words =
new
String[]{
"Hello"
,
"World"
};
List<String> collect = Stream.of(words).map(i -> i.split(
""
)).flatMap(Stream::of).collect(toList());
|
或者:
1
|
List<String> collect = Stream.of(words).flatMap(word -> Stream.of(word.split(
""
))).collect(toList());
|
結果輸出:HeloWrd
使用flatMap方法的效果是,各個數組並不是分別映射一個流,而是映射成流的內容,所有使用map(Array::stream)時生成的單個流被合並起來,即扁平化為一個流。
下圖是運用flatMap的stream運行流程:
測試代碼:
public static void main(String[] args) { List<String> list = Arrays.asList("hello","world"); String[] words = new String[]{"Hello","World"}; //List<String[]> temp = list.stream().map(s -> s.split("")).distinct().collect(Collectors.toList()); List<String[]> temp = Stream.of(words).map(s -> s.split("")).distinct().collect(Collectors.toList()); System.out.println(JSON.toJSONString(temp)); //List<String> temp2 = list.stream().map(s->s.split("")).flatMap(Arrays::stream).distinct().collect(Collectors.toList()); List<String> temp2 = Arrays.stream(words).map(s -> s.split("")).flatMap(Stream::of).distinct().collect(Collectors.toList()); System.out.println(JSON.toJSONString(temp2)); }