java8 Stream map和flatmap的區別
阿新 • • 發佈:2020-12-02
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)); }