java8Stream map和flatmap的区别

java筑基 · 2022-03-09 · 254 人浏览

原文地址:https://www.cnblogs.com/wangjing666/p/9999666.html

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 =&nbsp;
new
 
String[]{
&quot;Hello&quot;
,
&quot;World&quot;
};
List&lt;String&gt; a = Arrays.stream(words)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
.map(word -&gt; word.split(
&quot;&quot;
))
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
.flatMap(Arrays::stream)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
.distinct()
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
.collect(toList());
a.forEach(System.out::print);

或者:

1
2
String[] words =&nbsp;
new
 
String[]{
&quot;Hello&quot;
,
&quot;World&quot;
};
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
List&lt;String&gt; collect = Stream.of(words).map(i -&gt; i.split(
&quot;&quot;
)).flatMap(Stream::of).collect(toList());

 

或者:

1
List&lt;String&gt; collect = Stream.of(words).flatMap(word -&gt; Stream.of(word.split(
&quot;&quot;
))).collect(toList());

  

结果输出:HeloWrd

        使用flatMap方法的效果是,各个数组并不是分别映射一个流,而是映射成流的内容,所有使用map(Array::stream)时生成的单个流被合并起来,即扁平化为一个流。

    下图是运用flatMap的stream运行流程,