Java8 Stream
- 1 Stream概述
- 2 Stream的创建
- 3 Stream的使用
- 案例使用的员工类
- 3.1 遍历/匹配(foreach/find/match)
- 3.2 筛选(filter)
- 3.3 聚合(max/min/count)
- 3.4 映射(map/flatMap)
- 3.5 归约(reduce)
- 3.6 收集(collect)
- 3.6.1 归集(toList/toSet/toMap)
- 3.6.2 统计(count/averaging)
- 3.6.3 分组(partitioningBy/groupingBy)
- 3.6.4 接合(joining)
- 3.6.5 归约(reducing)
- 3.7 排序(sorted)
- 3.8 提取/组合
- 4 Stream源码解读
点波关注不迷路,一键三连好运连连!
先贴上几个案例,水平高超的同学可以挑战一下:
- 从员工集合中筛选出salary大于8000的员工,并放置到新的集合里。
- 统计员工的最高薪资、平均薪资、薪资之和。
- 将员工按薪资从高到低排序,同样薪资者年龄小者在前。
- 将员工按性别分类,将员工按性别和地区分类,将员工按薪资是否高于8000分为两部分。
用传统的迭代处理也不是很难,但代码就显得冗余了,跟Stream相比高下立判。
1 Stream概述
Java 8 是一个非常成功的版本,这个版本新增的Stream
,配合同版本出现的 Lambda
,给我们操作集合(Collection)提供了极大的便利。
那么什么是Stream
?
Stream
将要处理的元素集合看作一种流,在流的过程中,借助Stream API
对流中的元素进行操作,比如:筛选、排序、聚合等。
Stream
可以由数组或集合创建,对流的操作分为两种:
- 中间操作,每次返回一个新的流,可以有多个。
- 终端操作,每个流只能进行一次终端操作,终端操作结束后流无法再次使用。终端操作会产生一个新的集合或值。
另外,Stream
有几个特性:
- stream不存储数据,而是按照特定的规则对数据进行计算,一般会输出结果。
- stream不会改变数据源,通常情况下会产生一个新的集合或一个值。
- stream具有延迟执行特性,只有调用终端操作时,中间操作才会执行。
2 Stream的创建
Stream
可以通过集合数组创建。
1、通过 java.util.Collection.stream()
方法用集合创建流
<span style="color:#000000"><code class="language-java">List<span style="color:#3d8fd1"><span style="color:#999999"><</span>String<span style="color:#999999">></span></span> list <span style="color:#ac9739">=</span> Arrays<span style="color:#999999">.</span><span style="color:#3d8fd1">asList</span><span style="color:#999999">(</span><span style="color:#ac9739">"a"</span><span style="color:#999999">,</span> <span style="color:#ac9739">"b"</span><span style="color:#999999">,</span> <span style="color:#ac9739">"c"</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#6b7394">// 创建一个顺序流</span>
Stream<span style="color:#3d8fd1"><span style="color:#999999"><</span>String<span style="color:#999999">></span></span> stream <span style="color:#ac9739">=</span> list<span style="color:#999999">.</span><span style="color:#3d8fd1">stream</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#6b7394">// 创建一个并行流</span>
Stream<span style="color:#3d8fd1"><span style="color:#999999"><</span>String<span style="color:#999999">></span></span> parallelStream <span style="color:#ac9739">=</span> list<span style="color:#999999">.</span><span style="color:#3d8fd1">parallelStream</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
</code></span>
- 1
- 2
- 3
- 4
- 5
2、使用java.util.Arrays.stream(T[] array)
方法用数组创建流
<span style="color:#000000"><code class="language-java"><span style="color:#6679cc">int</span><span style="color:#999999">[</span><span style="color:#999999">]</span> array<span style="color:#ac9739">=</span><span style="color:#999999">{</span><span style="color:#ac9739">1</span><span style="color:#999999">,</span><span style="color:#ac9739">3</span><span style="color:#999999">,</span><span style="color:#ac9739">5</span><span style="color:#999999">,</span><span style="color:#ac9739">6</span><span style="color:#999999">,</span><span style="color:#ac9739">8</span><span style="color:#999999">}</span><span style="color:#999999">;</span>
IntStream stream <span style="color:#ac9739">=</span> Arrays<span style="color:#999999">.</span><span style="color:#3d8fd1">stream</span><span style="color:#999999">(</span>array<span style="color:#999999">)</span><span style="color:#999999">;</span>
</code></span>
- 1
- 2
3、使用Stream
的静态方法:of()、iterate()、generate()
<span style="color:#000000"><code class="language-java">Stream<span style="color:#3d8fd1"><span style="color:#999999"><</span>Integer<span style="color:#999999">></span></span> stream <span style="color:#ac9739">=</span> Stream<span style="color:#999999">.</span><span style="color:#3d8fd1">of</span><span style="color:#999999">(</span><span style="color:#ac9739">1</span><span style="color:#999999">,</span> <span style="color:#ac9739">2</span><span style="color:#999999">,</span> <span style="color:#ac9739">3</span><span style="color:#999999">,</span> <span style="color:#ac9739">4</span><span style="color:#999999">,</span> <span style="color:#ac9739">5</span><span style="color:#999999">,</span> <span style="color:#ac9739">6</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
Stream<span style="color:#3d8fd1"><span style="color:#999999"><</span>Integer<span style="color:#999999">></span></span> stream2 <span style="color:#ac9739">=</span> Stream<span style="color:#999999">.</span><span style="color:#3d8fd1">iterate</span><span style="color:#999999">(</span><span style="color:#ac9739">0</span><span style="color:#999999">,</span> <span style="color:#999999">(</span>x<span style="color:#999999">)</span> <span style="color:#ac9739">-</span><span style="color:#ac9739">></span> x <span style="color:#ac9739">+</span> <span style="color:#ac9739">3</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#3d8fd1">limit</span><span style="color:#999999">(</span><span style="color:#ac9739">4</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
stream2<span style="color:#999999">.</span><span style="color:#3d8fd1">forEach</span><span style="color:#999999">(</span>System<span style="color:#999999">.</span>out<span style="color:#ac9739">:</span><span style="color:#ac9739">:</span>println<span style="color:#999999">)</span><span style="color:#999999">;</span>
Stream<span style="color:#3d8fd1"><span style="color:#999999"><</span>Double<span style="color:#999999">></span></span> stream3 <span style="color:#ac9739">=</span> Stream<span style="color:#999999">.</span><span style="color:#3d8fd1">generate</span><span style="color:#999999">(</span>Math<span style="color:#ac9739">:</span><span style="color:#ac9739">:</span>random<span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#3d8fd1">limit</span><span style="color:#999999">(</span><span style="color:#ac9739">3</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
stream3<span style="color:#999999">.</span><span style="color:#3d8fd1">forEach</span><span style="color:#999999">(</span>System<span style="color:#999999">.</span>out<span style="color:#ac9739">:</span><span style="color:#ac9739">:</span>println<span style="color:#999999">)</span><span style="color:#999999">;</span>
</code></span>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
输出结果:
0 3 6 9
0.6796156909271994
0.1914314208854283
0.8116932592396652
stream
和parallelStream
的简单区分: stream
是顺序流,由主线程按顺序对流执行操作,而parallelStream
是并行流,内部以多线程并行执行的方式对流进行操作,但前提是流中的数据处理没有顺序要求。例如筛选集合中的奇数,两者的处理不同之处:
如果流中的数据量足够大,并行流可以加快处速度。
除了直接创建并行流,还可以通过parallel()
把顺序流转换成并行流:
<span style="color:#000000"><code class="language-java">Optional<span style="color:#3d8fd1"><span style="color:#999999"><</span>Integer<span style="color:#999999">></span></span> findFirst <span style="color:#ac9739">=</span> list<span style="color:#999999">.</span><span style="color:#3d8fd1">stream</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#3d8fd1">parallel</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#3d8fd1">filter</span><span style="color:#999999">(</span>x<span style="color:#ac9739">-</span><span style="color:#ac9739">></span>x<span style="color:#ac9739">></span><span style="color:#ac9739">6</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#3d8fd1">findFirst</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
</code></span>
- 1
3 Stream的使用
在使用stream之前,先理解一个概念:Optional
。
Optional
类是一个可以为null
的容器对象。如果值存在则isPresent()
方法会返回true
,调用get()
方法会返回该对象。
更详细说明请见:菜鸟教程Java 8 Optional类
接下来,大批代码向你袭来!我将用20个案例将Stream的使用整得明明白白,只要跟着敲一遍代码,就能很好地掌握。
案例使用的员工类
这是后面案例中使用的员工类:
<span style="color:#000000"><code class="language-java">List<span style="color:#3d8fd1"><span style="color:#999999"><</span>Person<span style="color:#999999">></span></span> personList <span style="color:#ac9739">=</span> <span style="color:#6679cc">new</span> ArrayList<span style="color:#3d8fd1"><span style="color:#999999"><</span>Person<span style="color:#999999">></span></span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
personList<span style="color:#999999">.</span><span style="color:#3d8fd1">add</span><span style="color:#999999">(</span><span style="color:#6679cc">new</span> Person<span style="color:#999999">(</span><span style="color:#ac9739">"Tom"</span><span style="color:#999999">,</span> <span style="color:#ac9739">8900</span><span style="color:#999999">,</span> <span style="color:#ac9739">"male"</span><span style="color:#999999">,</span> <span style="color:#ac9739">"New York"</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
personList<span style="color:#999999">.</span><span style="color:#3d8fd1">add</span><span style="color:#999999">(</span><span style="color:#6679cc">new</span> Person<span style="color:#999999">(</span><span style="color:#ac9739">"Jack"</span><span style="color:#999999">,</span> <span style="color:#ac9739">7000</span><span style="color:#999999">,</span> <span style="color:#ac9739">"male"</span><span style="color:#999999">,</span> <span style="color:#ac9739">"Washington"</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
personList<span style="color:#999999">.</span><span style="color:#3d8fd1">add</span><span style="color:#999999">(</span><span style="color:#6679cc">new</span> Person<span style="color:#999999">(</span><span style="color:#ac9739">"Lily"</span><span style="color:#999999">,</span> <span style="color:#ac9739">7800</span><span style="color:#999999">,</span> <span style="color:#ac9739">"female"</span><span style="color:#999999">,</span> <span style="color:#ac9739">"Washington"</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
personList<span style="color:#999999">.</span><span style="color:#3d8fd1">add</span><span style="color:#999999">(</span><span style="color:#6679cc">new</span> Person<span style="color:#999999">(</span><span style="color:#ac9739">"Anni"</span><span style="color:#999999">,</span> <span style="color:#ac9739">8200</span><span style="color:#999999">,</span> <span style="color:#ac9739">"female"</span><span style="color:#999999">,</span> <span style="color:#ac9739">"New York"</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
personList<span style="color:#999999">.</span><span style="color:#3d8fd1">add</span><span style="color:#999999">(</span><span style="color:#6679cc">new</span> Person<span style="color:#999999">(</span><span style="color:#ac9739">"Owen"</span><span style="color:#999999">,</span> <span style="color:#ac9739">9500</span><span style="color:#999999">,</span> <span style="color:#ac9739">"male"</span><span style="color:#999999">,</span> <span style="color:#ac9739">"New York"</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
personList<span style="color:#999999">.</span><span style="color:#3d8fd1">add</span><span style="color:#999999">(</span><span style="color:#6679cc">new</span> Person<span style="color:#999999">(</span><span style="color:#ac9739">"Alisa"</span><span style="color:#999999">,</span> <span style="color:#ac9739">7900</span><span style="color:#999999">,</span> <span style="color:#ac9739">"female"</span><span style="color:#999999">,</span> <span style="color:#ac9739">"New York"</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#6679cc">class</span> Person <span style="color:#999999">{</span>
<span style="color:#6679cc">private</span> String name<span style="color:#999999">;</span> <span style="color:#6b7394">// 姓名</span>
<span style="color:#6679cc">private</span> <span style="color:#6679cc">int</span> salary<span style="color:#999999">;</span> <span style="color:#6b7394">// 薪资</span>
<span style="color:#6679cc">private</span> <span style="color:#6679cc">int</span> age<span style="color:#999999">;</span> <span style="color:#6b7394">// 年龄</span>
<span style="color:#6679cc">private</span> String sex<span style="color:#999999">;</span> <span style="color:#6b7394">//性别</span>
<span style="color:#6679cc">private</span> String area<span style="color:#999999">;</span> <span style="color:#6b7394">// 地区</span>
<span style="color:#6b7394">// 构造方法</span>
<span style="color:#6679cc">public</span> <span style="color:#3d8fd1">Person</span><span style="color:#999999">(</span>String name<span style="color:#999999">,</span> <span style="color:#6679cc">int</span> salary<span style="color:#999999">,</span> <span style="color:#6679cc">int</span> age<span style="color:#999999">,</span>String sex<span style="color:#999999">,</span>String area<span style="color:#999999">)</span> <span style="color:#999999">{</span>
<span style="color:#6679cc">this</span><span style="color:#999999">.</span>name <span style="color:#ac9739">=</span> name<span style="color:#999999">;</span>
<span style="color:#6679cc">this</span><span style="color:#999999">.</span>salary <span style="color:#ac9739">=</span> salary<span style="color:#999999">;</span>
<span style="color:#6679cc">this</span><span style="color:#999999">.</span>age <span style="color:#ac9739">=</span> age<span style="color:#999999">;</span>
<span style="color:#6679cc">this</span><span style="color:#999999">.</span>sex <span style="color:#ac9739">=</span> sex<span style="color:#999999">;</span>
<span style="color:#6679cc">this</span><span style="color:#999999">.</span>area <span style="color:#ac9739">=</span> area<span style="color:#999999">;</span>
<span style="color:#999999">}</span>
<span style="color:#6b7394">// 省略了get和set,请自行添加</span>
<span style="color:#999999">}</span>
</code></span>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
3.1 遍历/匹配(foreach/find/match)
Stream
也是支持类似集合的遍历和匹配元素的,只是Stream
中的元素是以Optional
类型存在的。Stream
的遍历、匹配非常简单。
<span style="color:#000000"><code class="language-java"><span style="color:#6b7394">// import已省略,请自行添加,后面代码亦是</span>
<span style="color:#6679cc">public</span> <span style="color:#6679cc">class</span> StreamTest <span style="color:#999999">{</span>
<span style="color:#6679cc">public</span> <span style="color:#6679cc">static</span> <span style="color:#6679cc">void</span> <span style="color:#3d8fd1">main</span><span style="color:#999999">(</span>String<span style="color:#999999">[</span><span style="color:#999999">]</span> args<span style="color:#999999">)</span> <span style="color:#999999">{</span>
List<span style="color:#3d8fd1"><span style="color:#999999"><</span>Integer<span style="color:#999999">></span></span> list <span style="color:#ac9739">=</span> Arrays<span style="color:#999999">.</span><span style="color:#3d8fd1">asList</span><span style="color:#999999">(</span><span style="color:#ac9739">7</span><span style="color:#999999">,</span> <span style="color:#ac9739">6</span><span style="color:#999999">,</span> <span style="color:#ac9739">9</span><span style="color:#999999">,</span> <span style="color:#ac9739">3</span><span style="color:#999999">,</span> <span style="color:#ac9739">8</span><span style="color:#999999">,</span> <span style="color:#ac9739">2</span><span style="color:#999999">,</span> <span style="color:#ac9739">1</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#6b7394">// 遍历输出符合条件的元素</span>
list<span style="color:#999999">.</span><span style="color:#3d8fd1">stream</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#3d8fd1">filter</span><span style="color:#999999">(</span>x <span style="color:#ac9739">-</span><span style="color:#ac9739">></span> x <span style="color:#ac9739">></span> <span style="color:#ac9739">6</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#3d8fd1">forEach</span><span style="color:#999999">(</span>System<span style="color:#999999">.</span>out<span style="color:#ac9739">:</span><span style="color:#ac9739">:</span>println<span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#6b7394">// 匹配第一个</span>
Optional<span style="color:#3d8fd1"><span style="color:#999999"><</span>Integer<span style="color:#999999">></span></span> findFirst <span style="color:#ac9739">=</span> list<span style="color:#999999">.</span><span style="color:#3d8fd1">stream</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#3d8fd1">filter</span><span style="color:#999999">(</span>x <span style="color:#ac9739">-</span><span style="color:#ac9739">></span> x <span style="color:#ac9739">></span> <span style="color:#ac9739">6</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#3d8fd1">findFirst</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#6b7394">// 匹配任意(适用于并行流)</span>
Optional<span style="color:#3d8fd1"><span style="color:#999999"><</span>Integer<span style="color:#999999">></span></span> findAny <span style="color:#ac9739">=</span> list<span style="color:#999999">.</span><span style="color:#3d8fd1">parallelStream</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#3d8fd1">filter</span><span style="color:#999999">(</span>x <span style="color:#ac9739">-</span><span style="color:#ac9739">></span> x <span style="color:#ac9739">></span> <span style="color:#ac9739">6</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#3d8fd1">findAny</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#6b7394">// 是否包含符合特定条件的元素</span>
<span style="color:#6679cc">boolean</span> anyMatch <span style="color:#ac9739">=</span> list<span style="color:#999999">.</span><span style="color:#3d8fd1">stream</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#3d8fd1">anyMatch</span><span style="color:#999999">(</span>x <span style="color:#ac9739">-</span><span style="color:#ac9739">></span> x <span style="color:#ac9739"><</span> <span style="color:#ac9739">6</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#3d8fd1">println</span><span style="color:#999999">(</span><span style="color:#ac9739">"匹配第一个值:"</span> <span style="color:#ac9739">+</span> findFirst<span style="color:#999999">.</span><span style="color:#3d8fd1">get</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#3d8fd1">println</span><span style="color:#999999">(</span><span style="color:#ac9739">"匹配任意一个值:"</span> <span style="color:#ac9739">+</span> findAny<span style="color:#999999">.</span><span style="color:#3d8fd1">get</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#3d8fd1">println</span><span style="color:#999999">(</span><span style="color:#ac9739">"是否存在大于6的值:"</span> <span style="color:#ac9739">+</span> anyMatch<span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#999999">}</span>
<span style="color:#999999">}</span>
</code></span>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
3.2 筛选(filter)
筛选,是按照一定的规则校验流中的元素,将符合条件的元素提取到新的流中的操作。
案例一:筛选出Integer
集合中大于7的元素,并打印出来
<span style="color:#000000"><code class="language-java"><span style="color:#6679cc">public</span> <span style="color:#6679cc">class</span> StreamTest <span style="color:#999999">{</span>
<span style="color:#6679cc">public</span> <span style="color:#6679cc">static</span> <span style="color:#6679cc">void</span> <span style="color:#3d8fd1">main</span><span style="color:#999999">(</span>String<span style="color:#999999">[</span><span style="color:#999999">]</span> args<span style="color:#999999">)</span> <span style="color:#999999">{</span>
List<span style="color:#3d8fd1"><span style="color:#999999"><</span>Integer<span style="color:#999999">></span></span> list <span style="color:#ac9739">=</span> Arrays<span style="color:#999999">.</span><span style="color:#3d8fd1">asList</span><span style="color:#999999">(</span><span style="color:#ac9739">6</span><span style="color:#999999">,</span> <span style="color:#ac9739">7</span><span style="color:#999999">,</span> <span style="color:#ac9739">3</span><span style="color:#999999">,</span> <span style="color:#ac9739">8</span><span style="color:#999999">,</span> <span style="color:#ac9739">1</span><span style="color:#999999">,</span> <span style="color:#ac9739">2</span><span style="color:#999999">,</span> <span style="color:#ac9739">9</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
Stream<span style="color:#3d8fd1"><span style="color:#999999"><</span>Integer<span style="color:#999999">></span></span> stream <span style="color:#ac9739">=</span> list<span style="color:#999999">.</span><span style="color:#3d8fd1">stream</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
stream<span style="color:#999999">.</span><span style="color:#3d8fd1">filter</span><span style="color:#999999">(</span>x <span style="color:#ac9739">-</span><span style="color:#ac9739">></span> x <span style="color:#ac9739">></span> <span style="color:#ac9739">7</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#3d8fd1">forEach</span><span style="color:#999999">(</span>System<span style="color:#999999">.</span>out<span style="color:#ac9739">:</span><span style="color:#ac9739">:</span>println<span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#999999">}</span>
<span style="color:#999999">}</span>
</code></span>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
预期结果:
8 9
案例二: 筛选员工中工资高于8000的人,并形成新的集合。 形成新集合依赖collect
(收集),后文有详细介绍。
<span style="color:#000000"><code class="language-java"><span style="color:#6679cc">public</span> <span style="color:#6679cc">class</span> StreamTest <span style="color:#999999">{</span>
<span style="color:#6679cc">public</span> <span style="color:#6679cc">static</span> <span style="color:#6679cc">void</span> <span style="color:#3d8fd1">main</span><span style="color:#999999">(</span>String<span style="color:#999999">[</span><span style="color:#999999">]</span> args<span style="color:#999999">)</span> <span style="color:#999999">{</span>
List<span style="color:#3d8fd1"><span style="color:#999999"><</span>Person<span style="color:#999999">></span></span> personList <span style="color:#ac9739">=</span> <span style="color:#6679cc">new</span> ArrayList<span style="color:#3d8fd1"><span style="color:#999999"><</span>Person<span style="color:#999999">></span></span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
personList<span style="color:#999999">.</span><span style="color:#3d8fd1">add</span><span style="color:#999999">(</span><span style="color:#6679cc">new</span> Person<span style="color:#999999">(</span><span style="color:#ac9739">"Tom"</span><span style="color:#999999">,</span> <span style="color:#ac9739">8900</span><span style="color:#999999">,</span> <span style="color:#ac9739">23</span><span style="color:#999999">,</span> <span style="color:#ac9739">"male"</span><span style="color:#999999">,</span> <span style="color:#ac9739">"New York"</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
personList<span style="color:#999999">.</span><span style="color:#3d8fd1">add</span><span style="color:#999999">(</span><span style="color:#6679cc">new</span> Person<span style="color:#999999">(</span><span style="color:#ac9739">"Jack"</span><span style="color:#999999">,</span> <span style="color:#ac9739">7000</span><span style="color:#999999">,</span> <span style="color:#ac9739">25</span><span style="color:#999999">,</span> <span style="color:#ac9739">"male"</span><span style="color:#999999">,</span> <span style="color:#ac9739">"Washington"</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
personList<span style="color:#999999">.</span><span style="color:#3d8fd1">add</span><span style="color:#999999">(</span><span style="color:#6679cc">new</span> Person<span style="color:#999999">(</span><span style="color:#ac9739">"Lily"</span><span style="color:#999999">,</span> <span style="color:#ac9739">7800</span><span style="color:#999999">,</span> <span style="color:#ac9739">21</span><span style="color:#999999">,</span> <span style="color:#ac9739">"female"</span><span style="color:#999999">,</span> <span style="color:#ac9739">"Washington"</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
personList<span style="color:#999999">.</span><span style="color:#3d8fd1">add</span><span style="color:#999999">(</span><span style="color:#6679cc">new</span> Person<span style="color:#999999">(</span><span style="color:#ac9739">"Anni"</span><span style="color:#999999">,</span> <span style="color:#ac9739">8200</span><span style="color:#999999">,</span> <span style="color:#ac9739">24</span><span style="color:#999999">,</span> <span style="color:#ac9739">"female"</span><span style="color:#999999">,</span> <span style="color:#ac9739">"New York"</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
personList<span style="color:#999999">.</span><span style="color:#3d8fd1">add</span><span style="color:#999999">(</span><span style="color:#6679cc">new</span> Person<span style="color:#999999">(</span><span style="color:#ac9739">"Owen"</span><span style="color:#999999">,</span> <span style="color:#ac9739">9500</span><span style="color:#999999">,</span> <span style="color:#ac9739">25</span><span style="color:#999999">,</span> <span style="color:#ac9739">"male"</span><span style="color:#999999">,</span> <span style="color:#ac9739">"New York"</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
personList<span style="color:#999999">.</span><span style="color:#3d8fd1">add</span><span style="color:#999999">(</span><span style="color:#6679cc">new</span> Person<span style="color:#999999">(</span><span style="color:#ac9739">"Alisa"</span><span style="color:#999999">,</span> <span style="color:#ac9739">7900</span><span style="color:#999999">,</span> <span style="color:#ac9739">26</span><span style="color:#999999">,</span> <span style="color:#ac9739">"female"</span><span style="color:#999999">,</span> <span style="color:#ac9739">"New York"</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
List<span style="color:#3d8fd1"><span style="color:#999999"><</span>String<span style="color:#999999">></span></span> fiterList <span style="color:#ac9739">=</span> personList<span style="color:#999999">.</span><span style="color:#3d8fd1">stream</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#3d8fd1">filter</span><span style="color:#999999">(</span>x <span style="color:#ac9739">-</span><span style="color:#ac9739">></span> x<span style="color:#999999">.</span><span style="color:#3d8fd1">getSalary</span><span style="color:#999999">(</span><span style="color:#999999">)</span> <span style="color:#ac9739">></span> <span style="color:#ac9739">8000</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#3d8fd1">map</span><span style="color:#999999">(</span>Person<span style="color:#ac9739">:</span><span style="color:#ac9739">:</span>getName<span style="color:#999999">)</span>
<span style="color:#999999">.</span><span style="color:#3d8fd1">collect</span><span style="color:#999999">(</span>Collectors<span style="color:#999999">.</span><span style="color:#3d8fd1">toList</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#3d8fd1">print</span><span style="color:#999999">(</span><span style="color:#ac9739">"高于8000的员工姓名:"</span> <span style="color:#ac9739">+</span> fiterList<span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#999999">}</span>
<span style="color:#999999">}</span>
</code></span>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
运行结果:
高于8000的员工姓名:[Tom, Anni, Owen]
3.3 聚合(max/min/count)
max
、min
、count
这些字眼你一定不陌生,没错,在mysql中我们常用它们进行数据统计。Java stream中也引入了这些概念和用法,极大地方便了我们对集合、数组的数据统计工作。
案例一:获取String
集合中最长的元素。
<span style="color:#000000"><code class="language-java"><span style="color:#6679cc">public</span> <span style="color:#6679cc">class</span> StreamTest <span style="color:#999999">{</span>
<span style="color:#6679cc">public</span> <span style="color:#6679cc">static</span> <span style="color:#6679cc">void</span> <span style="color:#3d8fd1">main</span><span style="color:#999999">(</span>String<span style="color:#999999">[</span><span style="color:#999999">]</span> args<span style="color:#999999">)</span> <span style="color:#999999">{</span>
List<span style="color:#3d8fd1"><span style="color:#999999"><</span>String<span style="color:#999999">></span></span> list <span style="color:#ac9739">=</span> Arrays<span style="color:#999999">.</span><span style="color:#3d8fd1">asList</span><span style="color:#999999">(</span><span style="color:#ac9739">"adnm"</span><span style="color:#999999">,</span> <span style="color:#ac9739">"admmt"</span><span style="color:#999999">,</span> <span style="color:#ac9739">"pot"</span><span style="color:#999999">,</span> <span style="color:#ac9739">"xbangd"</span><span style="color:#999999">,</span> <span style="color:#ac9739">"weoujgsd"</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
Optional<span style="color:#3d8fd1"><span style="color:#999999"><</span>String<span style="color:#999999">></span></span> max <span style="color:#ac9739">=</span> list<span style="color:#999999">.</span><span style="color:#3d8fd1">stream</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#3d8fd1">max</span><span style="color:#999999">(</span>Comparator<span style="color:#999999">.</span><span style="color:#3d8fd1">comparing</span><span style="color:#999999">(</span>String<span style="color:#ac9739">:</span><span style="color:#ac9739">:</span>length<span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#3d8fd1">println</span><span style="color:#999999">(</span><span style="color:#ac9739">"最长的字符串:"</span> <span style="color:#ac9739">+</span> max<span style="color:#999999">.</span><span style="color:#3d8fd1">get</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#999999">}</span>
<span style="color:#999999">}</span>
</code></span>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
输出结果:
最长的字符串:weoujgsd
案例二:获取Integer
集合中的最大值。
<span style="color:#000000"><code class="language-java"><span style="color:#6679cc">public</span> <span style="color:#6679cc">class</span> StreamTest <span style="color:#999999">{</span>
<span style="color:#6679cc">public</span> <span style="color:#6679cc">static</span> <span style="color:#6679cc">void</span> <span style="color:#3d8fd1">main</span><span style="color:#999999">(</span>String<span style="color:#999999">[</span><span style="color:#999999">]</span> args<span style="color:#999999">)</span> <span style="color:#999999">{</span>
List<span style="color:#3d8fd1"><span style="color:#999999"><</span>Integer<span style="color:#999999">></span></span> list <span style="color:#ac9739">=</span> Arrays<span style="color:#999999">.</span><span style="color:#3d8fd1">asList</span><span style="color:#999999">(</span><span style="color:#ac9739">7</span><span style="color:#999999">,</span> <span style="color:#ac9739">6</span><span style="color:#999999">,</span> <span style="color:#ac9739">9</span><span style="color:#999999">,</span> <span style="color:#ac9739">4</span><span style="color:#999999">,</span> <span style="color:#ac9739">11</span><span style="color:#999999">,</span> <span style="color:#ac9739">6</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#6b7394">// 自然排序</span>
Optional<span style="color:#3d8fd1"><span style="color:#999999"><</span>Integer<span style="color:#999999">></span></span> max <span style="color:#ac9739">=</span> list<span style="color:#999999">.</span><span style="color:#3d8fd1">stream</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#3d8fd1">max</span><span style="color:#999999">(</span>Integer<span style="color:#ac9739">:</span><span style="color:#ac9739">:</span>compareTo<span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#6b7394">// 自定义排序</span>
Optional<span style="color:#3d8fd1"><span style="color:#999999"><</span>Integer<span style="color:#999999">></span></span> max2 <span style="color:#ac9739">=</span> list<span style="color:#999999">.</span><span style="color:#3d8fd1">stream</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#3d8fd1">max</span><span style="color:#999999">(</span><span style="color:#6679cc">new</span> Comparator<span style="color:#3d8fd1"><span style="color:#999999"><</span>Integer<span style="color:#999999">></span></span><span style="color:#999999">(</span><span style="color:#999999">)</span> <span style="color:#999999">{</span>
<span style="color:#999999">@Override</span>
<span style="color:#6679cc">public</span> <span style="color:#6679cc">int</span> <span style="color:#3d8fd1">compare</span><span style="color:#999999">(</span>Integer o1<span style="color:#999999">,</span> Integer o2<span style="color:#999999">)</span> <span style="color:#999999">{</span>
<span style="color:#6679cc">return</span> o1<span style="color:#999999">.</span><span style="color:#3d8fd1">compareTo</span><span style="color:#999999">(</span>o2<span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#999999">}</span>
<span style="color:#999999">}</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#3d8fd1">println</span><span style="color:#999999">(</span><span style="color:#ac9739">"自然排序的最大值:"</span> <span style="color:#ac9739">+</span> max<span style="color:#999999">.</span><span style="color:#3d8fd1">get</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#3d8fd1">println</span><span style="color:#999999">(</span><span style="color:#ac9739">"自定义排序的最大值:"</span> <span style="color:#ac9739">+</span> max2<span style="color:#999999">.</span><span style="color:#3d8fd1">get</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#999999">}</span>
<span style="color:#999999">}</span>
</code></span>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
输出结果:
自然排序的最大值:11
自定义排序的最大值:11
案例三:获取员工工资最高的人。
<span style="color:#000000"><code class="language-java"><span style="color:#6679cc">public</span> <span style="color:#6679cc">class</span> StreamTest <span style="color:#999999">{</span>
<span style="color:#6679cc">public</span> <span style="color:#6679cc">static</span> <span style="color:#6679cc">void</span> <span style="color:#3d8fd1">main</span><span style="color:#999999">(</span>String<span style="color:#999999">[</span><span style="color:#999999">]</span> args<span style="color:#999999">)</span> <span style="color:#999999">{</span>
List<span style="color:#3d8fd1"><span style="color:#999999"><</span>Person<span style="color:#999999">></span></span> personList <span style="color:#ac9739">=</span> <span style="color:#6679cc">new</span> ArrayList<span style="color:#3d8fd1"><span style="color:#999999"><</span>Person<span style="color:#999999">></span></span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
personList<span style="color:#999999">.</span><span style="color:#3d8fd1">add</span><span style="color:#999999">(</span><span style="color:#6679cc">new</span> Person<span style="color:#999999">(</span><span style="color:#ac9739">"Tom"</span><span style="color:#999999">,</span> <span style="color:#ac9739">8900</span><span style="color:#999999">,</span> <span style="color:#ac9739">23</span><span style="color:#999999">,</span> <span style="color:#ac9739">"male"</span><span style="color:#999999">,</span> <span style="color:#ac9739">"New York"</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
personList<span style="color:#999999">.</span><span style="color:#3d8fd1">add</span><span style="color:#999999">(</span><span style="color:#6679cc">new</span> Person<span style="color:#999999">(</span><span style="color:#ac9739">"Jack"</span><span style="color:#999999">,</span> <span style="color:#ac9739">7000</span><span style="color:#999999">,</span> <span style="color:#ac9739">25</span><span style="color:#999999">,</span> <span style="color:#ac9739">"male"</span><span style="color:#999999">,</span> <span style="color:#ac9739">"Washington"</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
personList<span style="color:#999999">.</span><span style="color:#3d8fd1">add</span><span style="color:#999999">(</span><span style="color:#6679cc">new</span> Person<span style="color:#999999">(</span><span style="color:#ac9739">"Lily"</span><span style="color:#999999">,</span> <span style="color:#ac9739">7800</span><span style="color:#999999">,</span> <span style="color:#ac9739">21</span><span style="color:#999999">,</span> <span style="color:#ac9739">"female"</span><span style="color:#999999">,</span> <span style="color:#ac9739">"Washington"</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
personList<span style="color:#999999">.</span><span style="color:#3d8fd1">add</span><span style="color:#999999">(</span><span style="color:#6679cc">new</span> Person<span style="color:#999999">(</span><span style="color:#ac9739">"Anni"</span><span style="color:#999999">,</span> <span style="color:#ac9739">8200</span><span style="color:#999999">,</span> <span style="color:#ac9739">24</span><span style="color:#999999">,</span> <span style="color:#ac9739">"female"</span><span style="color:#999999">,</span> <span style="color:#ac9739">"New York"</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
personList<span style="color:#999999">.</span><span style="color:#3d8fd1">add</span><span style="color:#999999">(</span><span style="color:#6679cc">new</span> Person<span style="color:#999999">(</span><span style="color:#ac9739">"Owen"</span><span style="color:#999999">,</span> <span style="color:#ac9739">9500</span><span style="color:#999999">,</span> <span style="color:#ac9739">25</span><span style="color:#999999">,</span> <span style="color:#ac9739">"male"</span><span style="color:#999999">,</span> <span style="color:#ac9739">"New York"</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
personList<span style="color:#999999">.</span><span style="color:#3d8fd1">add</span><span style="color:#999999">(</span><span style="color:#6679cc">new</span> Person<span style="color:#999999">(</span><span style="color:#ac9739">"Alisa"</span><span style="color:#999999">,</span> <span style="color:#ac9739">7900</span><span style="color:#999999">,</span> <span style="color:#ac9739">26</span><span style="color:#999999">,</span> <span style="color:#ac9739">"female"</span><span style="color:#999999">,</span> <span style="color:#ac9739">"New York"</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
Optional<span style="color:#3d8fd1"><span style="color:#999999"><</span>Person<span style="color:#999999">></span></span> max <span style="color:#ac9739">=</span> personList<span style="color:#999999">.</span><span style="color:#3d8fd1">stream</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#3d8fd1">max</span><span style="color:#999999">(</span>Comparator<span style="color:#999999">.</span><span style="color:#3d8fd1">comparingInt</span><span style="color:#999999">(</span>Person<span style="color:#ac9739">:</span><span style="color:#ac9739">:</span>getSalary<span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#3d8fd1">println</span><span style="color:#999999">(</span><span style="color:#ac9739">"员工工资最大值:"</span> <span style="color:#ac9739">+</span> max<span style="color:#999999">.</span><span style="color:#3d8fd1">get</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#3d8fd1">getSalary</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#999999">}</span>
<span style="color:#999999">}</span>
</code></span>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
输出结果:
员工工资最大值:9500
案例四:计算Integer
集合中大于6的元素的个数。
<span style="color:#000000"><code class="language-java"><span style="color:#6679cc">import</span> java<span style="color:#999999">.</span>util<span style="color:#999999">.</span>Arrays<span style="color:#999999">;</span>
<span style="color:#6679cc">import</span> java<span style="color:#999999">.</span>util<span style="color:#999999">.</span>List<span style="color:#999999">;</span>
<span style="color:#6679cc">public</span> <span style="color:#6679cc">class</span> StreamTest <span style="color:#999999">{</span>
<span style="color:#6679cc">public</span> <span style="color:#6679cc">static</span> <span style="color:#6679cc">void</span> <span style="color:#3d8fd1">main</span><span style="color:#999999">(</span>String<span style="color:#999999">[</span><span style="color:#999999">]</span> args<span style="color:#999999">)</span> <span style="color:#999999">{</span>
List<span style="color:#3d8fd1"><span style="color:#999999"><</span>Integer<span style="color:#999999">></span></span> list <span style="color:#ac9739">=</span> Arrays<span style="color:#999999">.</span><span style="color:#3d8fd1">asList</span><span style="color:#999999">(</span><span style="color:#ac9739">7</span><span style="color:#999999">,</span> <span style="color:#ac9739">6</span><span style="color:#999999">,</span> <span style="color:#ac9739">4</span><span style="color:#999999">,</span> <span style="color:#ac9739">8</span><span style="color:#999999">,</span> <span style="color:#ac9739">2</span><span style="color:#999999">,</span> <span style="color:#ac9739">11</span><span style="color:#999999">,</span> <span style="color:#ac9739">9</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#6679cc">long</span> count <span style="color:#ac9739">=</span> list<span style="color:#999999">.</span><span style="color:#3d8fd1">stream</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#3d8fd1">filter</span><span style="color:#999999">(</span>x <span style="color:#ac9739">-</span><span style="color:#ac9739">></span> x <span style="color:#ac9739">></span> <span style="color:#ac9739">6</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#3d8fd1">count</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#3d8fd1">println</span><span style="color:#999999">(</span><span style="color:#ac9739">"list中大于6的元素个数:"</span> <span style="color:#ac9739">+</span> count<span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#999999">}</span>
<span style="color:#999999">}</span>
</code></span>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
输出结果:
list中大于6的元素个数:4
3.4 映射(map/flatMap)
映射,可以将一个流的元素按照一定的映射规则映射到另一个流中。分为map
和flatMap
:
map
:接收一个函数作为参数,该函数会被应用到每个元素上,并将其映射成一个新的元素。flatMap
:接收一个函数作为参数,将流中的每个值都换成另一个流,然后把所有流连接成一个流。
案例一:英文字符串数组的元素全部改为大写。整数数组每个元素+3。
<span style="color:#000000"><code class="language-java"><span style="color:#6679cc">public</span> <span style="color:#6679cc">class</span> StreamTest <span style="color:#999999">{</span>
<span style="color:#6679cc">public</span> <span style="color:#6679cc">static</span> <span style="color:#6679cc">void</span> <span style="color:#3d8fd1">main</span><span style="color:#999999">(</span>String<span style="color:#999999">[</span><span style="color:#999999">]</span> args<span style="color:#999999">)</span> <span style="color:#999999">{</span>
String<span style="color:#999999">[</span><span style="color:#999999">]</span> strArr <span style="color:#ac9739">=</span> <span style="color:#999999">{</span> <span style="color:#ac9739">"abcd"</span><span style="color:#999999">,</span> <span style="color:#ac9739">"bcdd"</span><span style="color:#999999">,</span> <span style="color:#ac9739">"defde"</span><span style="color:#999999">,</span> <span style="color:#ac9739">"fTr"</span> <span style="color:#999999">}</span><span style="color:#999999">;</span>
List<span style="color:#3d8fd1"><span style="color:#999999"><</span>String<span style="color:#999999">></span></span> strList <span style="color:#ac9739">=</span> Arrays<span style="color:#999999">.</span><span style="color:#3d8fd1">stream</span><span style="color:#999999">(</span>strArr<span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#3d8fd1">map</span><span style="color:#999999">(</span>String<span style="color:#ac9739">:</span><span style="color:#ac9739">:</span>toUpperCase<span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#3d8fd1">collect</span><span style="color:#999999">(</span>Collectors<span style="color:#999999">.</span><span style="color:#3d8fd1">toList</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
List<span style="color:#3d8fd1"><span style="color:#999999"><</span>Integer<span style="color:#999999">></span></span> intList <span style="color:#ac9739">=</span> Arrays<span style="color:#999999">.</span><span style="color:#3d8fd1">asList</span><span style="color:#999999">(</span><span style="color:#ac9739">1</span><span style="color:#999999">,</span> <span style="color:#ac9739">3</span><span style="color:#999999">,</span> <span style="color:#ac9739">5</span><span style="color:#999999">,</span> <span style="color:#ac9739">7</span><span style="color:#999999">,</span> <span style="color:#ac9739">9</span><span style="color:#999999">,</span> <span style="color:#ac9739">11</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
List<span style="color:#3d8fd1"><span style="color:#999999"><</span>Integer<span style="color:#999999">></span></span> intListNew <span style="color:#ac9739">=</span> intList<span style="color:#999999">.</span><span style="color:#3d8fd1">stream</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#3d8fd1">map</span><span style="color:#999999">(</span>x <span style="color:#ac9739">-</span><span style="color:#ac9739">></span> x <span style="color:#ac9739">+</span> <span style="color:#ac9739">3</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#3d8fd1">collect</span><span style="color:#999999">(</span>Collectors<span style="color:#999999">.</span><span style="color:#3d8fd1">toList</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#3d8fd1">println</span><span style="color:#999999">(</span><span style="color:#ac9739">"每个元素大写:"</span> <span style="color:#ac9739">+</span> strList<span style="color:#999999">)</span><span style="color:#999999">;</span>
System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#3d8fd1">println</span><span style="color:#999999">(</span><span style="color:#ac9739">"每个元素+3:"</span> <span style="color:#ac9739">+</span> intListNew<span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#999999">}</span>
<span style="color:#999999">}</span>
</code></span>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
输出结果:
每个元素大写:[ABCD, BCDD, DEFDE, FTR]
每个元素+3:[4, 6, 8, 10, 12, 14]
案例二:将员工的薪资全部增加1000。
<span style="color:#000000"><code class="language-java"><span style="color:#6679cc">public</span> <span style="color:#6679cc">class</span> StreamTest <span style="color:#999999">{</span>
<span style="color:#6679cc">public</span> <span style="color:#6679cc">static</span> <span style="color:#6679cc">void</span> <span style="color:#3d8fd1">main</span><span style="color:#999999">(</span>String<span style="color:#999999">[</span><span style="color:#999999">]</span> args<span style="color:#999999">)</span> <span style="color:#999999">{</span>
List<span style="color:#3d8fd1"><span style="color:#999999"><</span>Person<span style="color:#999999">></span></span> personList <span style="color:#ac9739">=</span> <span style="color:#6679cc">new</span> ArrayList<span style="color:#3d8fd1"><span style="color:#999999"><</span>Person<span style="color:#999999">></span></span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
personList<span style="color:#999999">.</span><span style="color:#3d8fd1">add</span><span style="color:#999999">(</span><span style="color:#6679cc">new</span> Person<span style="color:#999999">(</span><span style="color:#ac9739">"Tom"</span><span style="color:#999999">,</span> <span style="color:#ac9739">8900</span><span style="color:#999999">,</span> <span style="color:#ac9739">23</span><span style="color:#999999">,</span> <span style="color:#ac9739">"male"</span><span style="color:#999999">,</span> <span style="color:#ac9739">"New York"</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
personList<span style="color:#999999">.</span><span style="color:#3d8fd1">add</span><span style="color:#999999">(</span><span style="color:#6679cc">new</span> Person<span style="color:#999999">(</span><span style="color:#ac9739">"Jack"</span><span style="color:#999999">,</span> <span style="color:#ac9739">7000</span><span style="color:#999999">,</span> <span style="color:#ac9739">25</span><span style="color:#999999">,</span> <span style="color:#ac9739">"male"</span><span style="color:#999999">,</span> <span style="color:#ac9739">"Washington"</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
personList<span style="color:#999999">.</span><span style="color:#3d8fd1">add</span><span style="color:#999999">(</span><span style="color:#6679cc">new</span> Person<span style="color:#999999">(</span><span style="color:#ac9739">"Lily"</span><span style="color:#999999">,</span> <span style="color:#ac9739">7800</span><span style="color:#999999">,</span> <span style="color:#ac9739">21</span><span style="color:#999999">,</span> <span style="color:#ac9739">"female"</span><span style="color:#999999">,</span> <span style="color:#ac9739">"Washington"</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
personList<span style="color:#999999">.</span><span style="color:#3d8fd1">add</span><span style="color:#999999">(</span><span style="color:#6679cc">new</span> Person<span style="color:#999999">(</span><span style="color:#ac9739">"Anni"</span><span style="color:#999999">,</span> <span style="color:#ac9739">8200</span><span style="color:#999999">,</span> <span style="color:#ac9739">24</span><span style="color:#999999">,</span> <span style="color:#ac9739">"female"</span><span style="color:#999999">,</span> <span style="color:#ac9739">"New York"</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
personList<span style="color:#999999">.</span><span style="color:#3d8fd1">add</span><span style="color:#999999">(</span><span style="color:#6679cc">new</span> Person<span style="color:#999999">(</span><span style="color:#ac9739">"Owen"</span><span style="color:#999999">,</span> <span style="color:#ac9739">9500</span><span style="color:#999999">,</span> <span style="color:#ac9739">25</span><span style="color:#999999">,</span> <span style="color:#ac9739">"male"</span><span style="color:#999999">,</span> <span style="color:#ac9739">"New York"</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
personList<span style="color:#999999">.</span><span style="color:#3d8fd1">add</span><span style="color:#999999">(</span><span style="color:#6679cc">new</span> Person<span style="color:#999999">(</span><span style="color:#ac9739">"Alisa"</span><span style="color:#999999">,</span> <span style="color:#ac9739">7900</span><span style="color:#999999">,</span> <span style="color:#ac9739">26</span><span style="color:#999999">,</span> <span style="color:#ac9739">"female"</span><span style="color:#999999">,</span> <span style="color:#ac9739">"New York"</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#6b7394">// 不改变原来员工集合的方式</span>
List<span style="color:#3d8fd1"><span style="color:#999999"><</span>Person<span style="color:#999999">></span></span> personListNew <span style="color:#ac9739">=</span> personList<span style="color:#999999">.</span><span style="color:#3d8fd1">stream</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#3d8fd1">map</span><span style="color:#999999">(</span>person <span style="color:#ac9739">-</span><span style="color:#ac9739">></span> <span style="color:#999999">{</span>
Person personNew <span style="color:#ac9739">=</span> <span style="color:#6679cc">new</span> Person<span style="color:#999999">(</span>person<span style="color:#999999">.</span><span style="color:#3d8fd1">getName</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">,</span> <span style="color:#ac9739">0</span><span style="color:#999999">,</span> <span style="color:#ac9739">0</span><span style="color:#999999">,</span> null<span style="color:#999999">,</span> null<span style="color:#999999">)</span><span style="color:#999999">;</span>
personNew<span style="color:#999999">.</span><span style="color:#3d8fd1">setSalary</span><span style="color:#999999">(</span>person<span style="color:#999999">.</span><span style="color:#3d8fd1">getSalary</span><span style="color:#999999">(</span><span style="color:#999999">)</span> <span style="color:#ac9739">+</span> <span style="color:#ac9739">10000</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#6679cc">return</span> personNew<span style="color:#999999">;</span>
<span style="color:#999999">}</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#3d8fd1">collect</span><span style="color:#999999">(</span>Collectors<span style="color:#999999">.</span><span style="color:#3d8fd1">toList</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#3d8fd1">println</span><span style="color:#999999">(</span><span style="color:#ac9739">"一次改动前:"</span> <span style="color:#ac9739">+</span> personList<span style="color:#999999">.</span><span style="color:#3d8fd1">get</span><span style="color:#999999">(</span><span style="color:#ac9739">0</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#3d8fd1">getName</span><span style="color:#999999">(</span><span style="color:#999999">)</span> <span style="color:#ac9739">+</span> <span style="color:#ac9739">"-->"</span> <span style="color:#ac9739">+</span> personList<span style="color:#999999">.</span><span style="color:#3d8fd1">get</span><span style="color:#999999">(</span><span style="color:#ac9739">0</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#3d8fd1">getSalary</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#3d8fd1">println</span><span style="color:#999999">(</span><span style="color:#ac9739">"一次改动后:"</span> <span style="color:#ac9739">+</span> personListNew<span style="color:#999999">.</span><span style="color:#3d8fd1">get</span><span style="color:#999999">(</span><span style="color:#ac9739">0</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#3d8fd1">getName</span><span style="color:#999999">(</span><span style="color:#999999">)</span> <span style="color:#ac9739">+</span> <span style="color:#ac9739">"-->"</span> <span style="color:#ac9739">+</span> personListNew<span style="color:#999999">.</span><span style="color:#3d8fd1">get</span><span style="color:#999999">(</span><span style="color:#ac9739">0</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#3d8fd1">getSalary</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#6b7394">// 改变原来员工集合的方式</span>
List<span style="color:#3d8fd1"><span style="color:#999999"><</span>Person<span style="color:#999999">></span></span> personListNew2 <span style="color:#ac9739">=</span> personList<span style="color:#999999">.</span><span style="color:#3d8fd1">stream</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#3d8fd1">map</span><span style="color:#999999">(</span>person <span style="color:#ac9739">-</span><span style="color:#ac9739">></span> <span style="color:#999999">{</span>
person<span style="color:#999999">.</span><span style="color:#3d8fd1">setSalary</span><span style="color:#999999">(</span>person<span style="color:#999999">.</span><span style="color:#3d8fd1">getSalary</span><span style="color:#999999">(</span><span style="color:#999999">)</span> <span style="color:#ac9739">+</span> <span style="color:#ac9739">10000</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#6679cc">return</span> person<span style="color:#999999">;</span>
<span style="color:#999999">}</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#3d8fd1">collect</span><span style="color:#999999">(</span>Collectors<span style="color:#999999">.</span><span style="color:#3d8fd1">toList</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#3d8fd1">println</span><span style="color:#999999">(</span><span style="color:#ac9739">"二次改动前:"</span> <span style="color:#ac9739">+</span> personList<span style="color:#999999">.</span><span style="color:#3d8fd1">get</span><span style="color:#999999">(</span><span style="color:#ac9739">0</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#3d8fd1">getName</span><span style="color:#999999">(</span><span style="color:#999999">)</span> <span style="color:#ac9739">+</span> <span style="color:#ac9739">"-->"</span> <span style="color:#ac9739">+</span> personListNew<span style="color:#999999">.</span><span style="color:#3d8fd1">get</span><span style="color:#999999">(</span><span style="color:#ac9739">0</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#3d8fd1">getSalary</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#3d8fd1">println</span><span style="color:#999999">(</span><span style="color:#ac9739">"二次改动后:"</span> <span style="color:#ac9739">+</span> personListNew2<span style="color:#999999">.</span><span style="color:#3d8fd1">get</span><span style="color:#999999">(</span><span style="color:#ac9739">0</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#3d8fd1">getName</span><span style="color:#999999">(</span><span style="color:#999999">)</span> <span style="color:#ac9739">+</span> <span style="color:#ac9739">"-->"</span> <span style="color:#ac9739">+</span> personListNew<span style="color:#999999">.</span><span style="color:#3d8fd1">get</span><span style="color:#999999">(</span><span style="color:#ac9739">0</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#3d8fd1">getSalary</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#999999">}</span>
<span style="color:#999999">}</span>
</code></span>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
输出结果:
一次改动前:Tom–>8900
一次改动后:Tom–>18900
二次改动前:Tom–>18900
二次改动后:Tom–>18900
案例三:将两个字符数组合并成一个新的字符数组。
<span style="color:#000000"><code class="language-java"><span style="color:#6679cc">public</span> <span style="color:#6679cc">class</span> StreamTest <span style="color:#999999">{</span>
<span style="color:#6679cc">public</span> <span style="color:#6679cc">static</span> <span style="color:#6679cc">void</span> <span style="color:#3d8fd1">main</span><span style="color:#999999">(</span>String<span style="color:#999999">[</span><span style="color:#999999">]</span> args<span style="color:#999999">)</span> <span style="color:#999999">{</span>
List<span style="color:#3d8fd1"><span style="color:#999999"><</span>String<span style="color:#999999">></span></span> list <span style="color:#ac9739">=</span> Arrays<span style="color:#999999">.</span><span style="color:#3d8fd1">asList</span><span style="color:#999999">(</span><span style="color:#ac9739">"m,k,l,a"</span><span style="color:#999999">,</span> <span style="color:#ac9739">"1,3,5,7"</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
List<span style="color:#3d8fd1"><span style="color:#999999"><</span>String<span style="color:#999999">></span></span> listNew <span style="color:#ac9739">=</span> list<span style="color:#999999">.</span><span style="color:#3d8fd1">stream</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#3d8fd1">flatMap</span><span style="color:#999999">(</span>s <span style="color:#ac9739">-</span><span style="color:#ac9739">></span> <span style="color:#999999">{</span>
<span style="color:#6b7394">// 将每个元素转换成一个stream</span>
String<span style="color:#999999">[</span><span style="color:#999999">]</span> split <span style="color:#ac9739">=</span> s<span style="color:#999999">.</span><span style="color:#3d8fd1">split</span><span style="color:#999999">(</span><span style="color:#ac9739">","</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
Stream<span style="color:#3d8fd1"><span style="color:#999999"><</span>String<span style="color:#999999">></span></span> s2 <span style="color:#ac9739">=</span> Arrays<span style="color:#999999">.</span><span style="color:#3d8fd1">stream</span><span style="color:#999999">(</span>split<span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#6679cc">return</span> s2<span style="color:#999999">;</span>
<span style="color:#999999">}</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#3d8fd1">collect</span><span style="color:#999999">(</span>Collectors<span style="color:#999999">.</span><span style="color:#3d8fd1">toList</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#3d8fd1">println</span><span style="color:#999999">(</span><span style="color:#ac9739">"处理前的集合:"</span> <span style="color:#ac9739">+</span> list<span style="color:#999999">)</span><span style="color:#999999">;</span>
System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#3d8fd1">println</span><span style="color:#999999">(</span><span style="color:#ac9739">"处理后的集合:"</span> <span style="color:#ac9739">+</span> listNew<span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#999999">}</span>
<span style="color:#999999">}</span>
</code></span>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
输出结果:
处理前的集合:[m-k-l-a, 1-3-5]
处理后的集合:[m, k, l, a, 1, 3, 5]
3.5 归约(reduce)
归约,也称缩减,顾名思义,是把一个流缩减成一个值,能实现对集合求和、求乘积和求最值操作。
案例一:求Integer
集合的元素之和、乘积和最大值。
<span style="color:#000000"><code class="language-java"><span style="color:#6679cc">public</span> <span style="color:#6679cc">class</span> StreamTest <span style="color:#999999">{</span>
<span style="color:#6679cc">public</span> <span style="color:#6679cc">static</span> <span style="color:#6679cc">void</span> <span style="color:#3d8fd1">main</span><span style="color:#999999">(</span>String<span style="color:#999999">[</span><span style="color:#999999">]</span> args<span style="color:#999999">)</span> <span style="color:#999999">{</span>
List<span style="color:#3d8fd1"><span style="color:#999999"><</span>Integer<span style="color:#999999">></span></span> list <span style="color:#ac9739">=</span> Arrays<span style="color:#999999">.</span><span style="color:#3d8fd1">asList</span><span style="color:#999999">(</span><span style="color:#ac9739">1</span><span style="color:#999999">,</span> <span style="color:#ac9739">3</span><span style="color:#999999">,</span> <span style="color:#ac9739">2</span><span style="color:#999999">,</span> <span style="color:#ac9739">8</span><span style="color:#999999">,</span> <span style="color:#ac9739">11</span><span style="color:#999999">,</span> <span style="color:#ac9739">4</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#6b7394">// 求和方式1</span>
Optional<span style="color:#3d8fd1"><span style="color:#999999"><</span>Integer<span style="color:#999999">></span></span> sum <span style="color:#ac9739">=</span> list<span style="color:#999999">.</span><span style="color:#3d8fd1">stream</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#3d8fd1">reduce</span><span style="color:#999999">(</span><span style="color:#999999">(</span>x<span style="color:#999999">,</span> y<span style="color:#999999">)</span> <span style="color:#ac9739">-</span><span style="color:#ac9739">></span> x <span style="color:#ac9739">+</span> y<span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#6b7394">// 求和方式2</span>
Optional<span style="color:#3d8fd1"><span style="color:#999999"><</span>Integer<span style="color:#999999">></span></span> sum2 <span style="color:#ac9739">=</span> list<span style="color:#999999">.</span><span style="color:#3d8fd1">stream</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#3d8fd1">reduce</span><span style="color:#999999">(</span>Integer<span style="color:#ac9739">:</span><span style="color:#ac9739">:</span>sum<span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#6b7394">// 求和方式3</span>
Integer sum3 <span style="color:#ac9739">=</span> list<span style="color:#999999">.</span><span style="color:#3d8fd1">stream</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#3d8fd1">reduce</span><span style="color:#999999">(</span><span style="color:#ac9739">0</span><span style="color:#999999">,</span> Integer<span style="color:#ac9739">:</span><span style="color:#ac9739">:</span>sum<span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#6b7394">// 求乘积</span>
Optional<span style="color:#3d8fd1"><span style="color:#999999"><</span>Integer<span style="color:#999999">></span></span> product <span style="color:#ac9739">=</span> list<span style="color:#999999">.</span><span style="color:#3d8fd1">stream</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#3d8fd1">reduce</span><span style="color:#999999">(</span><span style="color:#999999">(</span>x<span style="color:#999999">,</span> y<span style="color:#999999">)</span> <span style="color:#ac9739">-</span><span style="color:#ac9739">></span> x <span style="color:#ac9739">*</span> y<span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#6b7394">// 求最大值方式1</span>
Optional<span style="color:#3d8fd1"><span style="color:#999999"><</span>Integer<span style="color:#999999">></span></span> max <span style="color:#ac9739">=</span> list<span style="color:#999999">.</span><span style="color:#3d8fd1">stream</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#3d8fd1">reduce</span><span style="color:#999999">(</span><span style="color:#999999">(</span>x<span style="color:#999999">,</span> y<span style="color:#999999">)</span> <span style="color:#ac9739">-</span><span style="color:#ac9739">></span> x <span style="color:#ac9739">></span> y <span style="color:#ac9739">?</span> x <span style="color:#ac9739">:</span> y<span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#6b7394">// 求最大值写法2</span>
Integer max2 <span style="color:#ac9739">=</span> list<span style="color:#999999">.</span><span style="color:#3d8fd1">stream</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#3d8fd1">reduce</span><span style="color:#999999">(</span><span style="color:#ac9739">1</span><span style="color:#999999">,</span> Integer<span style="color:#ac9739">:</span><span style="color:#ac9739">:</span>max<span style="color:#999999">)</span><span style="color:#999999">;</span>
System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#3d8fd1">println</span><span style="color:#999999">(</span><span style="color:#ac9739">"list求和:"</span> <span style="color:#ac9739">+</span> sum<span style="color:#999999">.</span><span style="color:#3d8fd1">get</span><span style="color:#999999">(</span><span style="color:#999999">)</span> <span style="color:#ac9739">+</span> <span style="color:#ac9739">","</span> <span style="color:#ac9739">+</span> sum2<span style="color:#999999">.</span><span style="color:#3d8fd1">get</span><span style="color:#999999">(</span><span style="color:#999999">)</span> <span style="color:#ac9739">+</span> <span style="color:#ac9739">","</span> <span style="color:#ac9739">+</span> sum3<span style="color:#999999">)</span><span style="color:#999999">;</span>
System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#3d8fd1">println</span><span style="color:#999999">(</span><span style="color:#ac9739">"list求积:"</span> <span style="color:#ac9739">+</span> product<span style="color:#999999">.</span><span style="color:#3d8fd1">get</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#3d8fd1">println</span><span style="color:#999999">(</span><span style="color:#ac9739">"list求和:"</span> <span style="color:#ac9739">+</span> max<span style="color:#999999">.</span><span style="color:#3d8fd1">get</span><span style="color:#999999">(</span><span style="color:#999999">)</span> <span style="color:#ac9739">+</span> <span style="color:#ac9739">","</span> <span style="color:#ac9739">+</span> max2<span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#999999">}</span>
<span style="color:#999999">}</span>
</code></span>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
输出结果:
list求和:29,29,29
list求积:2112
list求和:11,11
案例二:求所有员工的工资之和和最高工资。
<span style="color:#000000"><code class="language-java"><span style="color:#6679cc">public</span> <span style="color:#6679cc">class</span> StreamTest <span style="color:#999999">{</span>
<span style="color:#6679cc">public</span> <span style="color:#6679cc">static</span> <span style="color:#6679cc">void</span> <span style="color:#3d8fd1">main</span><span style="color:#999999">(</span>String<span style="color:#999999">[</span><span style="color:#999999">]</span> args<span style="color:#999999">)</span> <span style="color:#999999">{</span>
List<span style="color:#3d8fd1"><span style="color:#999999"><</span>Person<span style="color:#999999">></span></span> personList <span style="color:#ac9739">=</span> <span style="color:#6679cc">new</span> ArrayList<span style="color:#3d8fd1"><span style="color:#999999"><</span>Person<span style="color:#999999">></span></span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
personList<span style="color:#999999">.</span><span style="color:#3d8fd1">add</span><span style="color:#999999">(</span><span style="color:#6679cc">new</span> Person<span style="color:#999999">(</span><span style="color:#ac9739">"Tom"</span><span style="color:#999999">,</span> <span style="color:#ac9739">8900</span><span style="color:#999999">,</span> <span style="color:#ac9739">23</span><span style="color:#999999">,</span> <span style="color:#ac9739">"male"</span><span style="color:#999999">,</span> <span style="color:#ac9739">"New York"</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
personList<span style="color:#999999">.</span><span style="color:#3d8fd1">add</span><span style="color:#999999">(</span><span style="color:#6679cc">new</span> Person<span style="color:#999999">(</span><span style="color:#ac9739">"Jack"</span><span style="color:#999999">,</span> <span style="color:#ac9739">7000</span><span style="color:#999999">,</span> <span style="color:#ac9739">25</span><span style="color:#999999">,</span> <span style="color:#ac9739">"male"</span><span style="color:#999999">,</span> <span style="color:#ac9739">"Washington"</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
personList<span style="color:#999999">.</span><span style="color:#3d8fd1">add</span><span style="color:#999999">(</span><span style="color:#6679cc">new</span> Person<span style="color:#999999">(</span><span style="color:#ac9739">"Lily"</span><span style="color:#999999">,</span> <span style="color:#ac9739">7800</span><span style="color:#999999">,</span> <span style="color:#ac9739">21</span><span style="color:#999999">,</span> <span style="color:#ac9739">"female"</span><span style="color:#999999">,</span> <span style="color:#ac9739">"Washington"</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
personList<span style="color:#999999">.</span><span style="color:#3d8fd1">add</span><span style="color:#999999">(</span><span style="color:#6679cc">new</span> Person<span style="color:#999999">(</span><span style="color:#ac9739">"Anni"</span><span style="color:#999999">,</span> <span style="color:#ac9739">8200</span><span style="color:#999999">,</span> <span style="color:#ac9739">24</span><span style="color:#999999">,</span> <span style="color:#ac9739">"female"</span><span style="color:#999999">,</span> <span style="color:#ac9739">"New York"</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
personList<span style="color:#999999">.</span><span style="color:#3d8fd1">add</span><span style="color:#999999">(</span><span style="color:#6679cc">new</span> Person<span style="color:#999999">(</span><span style="color:#ac9739">"Owen"</span><span style="color:#999999">,</span> <span style="color:#ac9739">9500</span><span style="color:#999999">,</span> <span style="color:#ac9739">25</span><span style="color:#999999">,</span> <span style="color:#ac9739">"male"</span><span style="color:#999999">,</span> <span style="color:#ac9739">"New York"</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
personList<span style="color:#999999">.</span><span style="color:#3d8fd1">add</span><span style="color:#999999">(</span><span style="color:#6679cc">new</span> Person<span style="color:#999999">(</span><span style="color:#ac9739">"Alisa"</span><span style="color:#999999">,</span> <span style="color:#ac9739">7900</span><span style="color:#999999">,</span> <span style="color:#ac9739">26</span><span style="color:#999999">,</span> <span style="color:#ac9739">"female"</span><span style="color:#999999">,</span> <span style="color:#ac9739">"New York"</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#6b7394">// 求工资之和方式1:</span>
Optional<span style="color:#3d8fd1"><span style="color:#999999"><</span>Integer<span style="color:#999999">></span></span> sumSalary <span style="color:#ac9739">=</span> personList<span style="color:#999999">.</span><span style="color:#3d8fd1">stream</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#3d8fd1">map</span><span style="color:#999999">(</span>Person<span style="color:#ac9739">:</span><span style="color:#ac9739">:</span>getSalary<span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#3d8fd1">reduce</span><span style="color:#999999">(</span>Integer<span style="color:#ac9739">:</span><span style="color:#ac9739">:</span>sum<span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#6b7394">// 求工资之和方式2:</span>
Integer sumSalary2 <span style="color:#ac9739">=</span> personList<span style="color:#999999">.</span><span style="color:#3d8fd1">stream</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#3d8fd1">reduce</span><span style="color:#999999">(</span><span style="color:#ac9739">0</span><span style="color:#999999">,</span> <span style="color:#999999">(</span>sum<span style="color:#999999">,</span> p<span style="color:#999999">)</span> <span style="color:#ac9739">-</span><span style="color:#ac9739">></span> sum <span style="color:#ac9739">+=</span> p<span style="color:#999999">.</span><span style="color:#3d8fd1">getSalary</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">,</span>
<span style="color:#999999">(</span>sum1<span style="color:#999999">,</span> sum2<span style="color:#999999">)</span> <span style="color:#ac9739">-</span><span style="color:#ac9739">></span> sum1 <span style="color:#ac9739">+</span> sum2<span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#6b7394">// 求工资之和方式3:</span>
Integer sumSalary3 <span style="color:#ac9739">=</span> personList<span style="color:#999999">.</span><span style="color:#3d8fd1">stream</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#3d8fd1">reduce</span><span style="color:#999999">(</span><span style="color:#ac9739">0</span><span style="color:#999999">,</span> <span style="color:#999999">(</span>sum<span style="color:#999999">,</span> p<span style="color:#999999">)</span> <span style="color:#ac9739">-</span><span style="color:#ac9739">></span> sum <span style="color:#ac9739">+=</span> p<span style="color:#999999">.</span><span style="color:#3d8fd1">getSalary</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">,</span> Integer<span style="color:#ac9739">:</span><span style="color:#ac9739">:</span>sum<span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#6b7394">// 求最高工资方式1:</span>
Integer maxSalary <span style="color:#ac9739">=</span> personList<span style="color:#999999">.</span><span style="color:#3d8fd1">stream</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#3d8fd1">reduce</span><span style="color:#999999">(</span><span style="color:#ac9739">0</span><span style="color:#999999">,</span> <span style="color:#999999">(</span>max<span style="color:#999999">,</span> p<span style="color:#999999">)</span> <span style="color:#ac9739">-</span><span style="color:#ac9739">></span> max <span style="color:#ac9739">></span> p<span style="color:#999999">.</span><span style="color:#3d8fd1">getSalary</span><span style="color:#999999">(</span><span style="color:#999999">)</span> <span style="color:#ac9739">?</span> max <span style="color:#ac9739">:</span> p<span style="color:#999999">.</span><span style="color:#3d8fd1">getSalary</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">,</span>
Integer<span style="color:#ac9739">:</span><span style="color:#ac9739">:</span>max<span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#6b7394">// 求最高工资方式2:</span>
Integer maxSalary2 <span style="color:#ac9739">=</span> personList<span style="color:#999999">.</span><span style="color:#3d8fd1">stream</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#3d8fd1">reduce</span><span style="color:#999999">(</span><span style="color:#ac9739">0</span><span style="color:#999999">,</span> <span style="color:#999999">(</span>max<span style="color:#999999">,</span> p<span style="color:#999999">)</span> <span style="color:#ac9739">-</span><span style="color:#ac9739">></span> max <span style="color:#ac9739">></span> p<span style="color:#999999">.</span><span style="color:#3d8fd1">getSalary</span><span style="color:#999999">(</span><span style="color:#999999">)</span> <span style="color:#ac9739">?</span> max <span style="color:#ac9739">:</span> p<span style="color:#999999">.</span><span style="color:#3d8fd1">getSalary</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">,</span>
<span style="color:#999999">(</span>max1<span style="color:#999999">,</span> max2<span style="color:#999999">)</span> <span style="color:#ac9739">-</span><span style="color:#ac9739">></span> max1 <span style="color:#ac9739">></span> max2 <span style="color:#ac9739">?</span> max1 <span style="color:#ac9739">:</span> max2<span style="color:#999999">)</span><span style="color:#999999">;</span>
System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#3d8fd1">println</span><span style="color:#999999">(</span><span style="color:#ac9739">"工资之和:"</span> <span style="color:#ac9739">+</span> sumSalary<span style="color:#999999">.</span><span style="color:#3d8fd1">get</span><span style="color:#999999">(</span><span style="color:#999999">)</span> <span style="color:#ac9739">+</span> <span style="color:#ac9739">","</span> <span style="color:#ac9739">+</span> sumSalary2 <span style="color:#ac9739">+</span> <span style="color:#ac9739">","</span> <span style="color:#ac9739">+</span> sumSalary3<span style="color:#999999">)</span><span style="color:#999999">;</span>
System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#3d8fd1">println</span><span style="color:#999999">(</span><span style="color:#ac9739">"最高工资:"</span> <span style="color:#ac9739">+</span> maxSalary <span style="color:#ac9739">+</span> <span style="color:#ac9739">","</span> <span style="color:#ac9739">+</span> maxSalary2<span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#999999">}</span>
<span style="color:#999999">}</span>
</code></span>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
输出结果:
工资之和:49300,49300,49300
最高工资:9500,9500
3.6 收集(collect)
collect
,收集,可以说是内容最繁多、功能最丰富的部分了。从字面上去理解,就是把一个流收集起来,最终可以是收集成一个值也可以收集成一个新的集合。
collect
主要依赖java.util.stream.Collectors
类内置的静态方法。
3.6.1 归集(toList/toSet/toMap)
因为流不存储数据,那么在流中的数据完成处理后,需要将流中的数据重新归集到新的集合里。toList
、toSet
和toMap
比较常用,另外还有toCollection
、toConcurrentMap
等复杂一些的用法。
下面用一个案例演示toList
、toSet
和toMap
:
<span style="color:#000000"><code class="language-java"><span style="color:#6679cc">public</span> <span style="color:#6679cc">class</span> StreamTest <span style="color:#999999">{</span>
<span style="color:#6679cc">public</span> <span style="color:#6679cc">static</span> <span style="color:#6679cc">void</span> <span style="color:#3d8fd1">main</span><span style="color:#999999">(</span>String<span style="color:#999999">[</span><span style="color:#999999">]</span> args<span style="color:#999999">)</span> <span style="color:#999999">{</span>
List<span style="color:#3d8fd1"><span style="color:#999999"><</span>Integer<span style="color:#999999">></span></span> list <span style="color:#ac9739">=</span> Arrays<span style="color:#999999">.</span><span style="color:#3d8fd1">asList</span><span style="color:#999999">(</span><span style="color:#ac9739">1</span><span style="color:#999999">,</span> <span style="color:#ac9739">6</span><span style="color:#999999">,</span> <span style="color:#ac9739">3</span><span style="color:#999999">,</span> <span style="color:#ac9739">4</span><span style="color:#999999">,</span> <span style="color:#ac9739">6</span><span style="color:#999999">,</span> <span style="color:#ac9739">7</span><span style="color:#999999">,</span> <span style="color:#ac9739">9</span><span style="color:#999999">,</span> <span style="color:#ac9739">6</span><span style="color:#999999">,</span> <span style="color:#ac9739">20</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
List<span style="color:#3d8fd1"><span style="color:#999999"><</span>Integer<span style="color:#999999">></span></span> listNew <span style="color:#ac9739">=</span> list<span style="color:#999999">.</span><span style="color:#3d8fd1">stream</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#3d8fd1">filter</span><span style="color:#999999">(</span>x <span style="color:#ac9739">-</span><span style="color:#ac9739">></span> x <span style="color:#ac9739">%</span> <span style="color:#ac9739">2</span> <span style="color:#ac9739">==</span> <span style="color:#ac9739">0</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#3d8fd1">collect</span><span style="color:#999999">(</span>Collectors<span style="color:#999999">.</span><span style="color:#3d8fd1">toList</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
Set<span style="color:#3d8fd1"><span style="color:#999999"><</span>Integer<span style="color:#999999">></span></span> set <span style="color:#ac9739">=</span> list<span style="color:#999999">.</span><span style="color:#3d8fd1">stream</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#3d8fd1">filter</span><span style="color:#999999">(</span>x <span style="color:#ac9739">-</span><span style="color:#ac9739">></span> x <span style="color:#ac9739">%</span> <span style="color:#ac9739">2</span> <span style="color:#ac9739">==</span> <span style="color:#ac9739">0</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#3d8fd1">collect</span><span style="color:#999999">(</span>Collectors<span style="color:#999999">.</span><span style="color:#3d8fd1">toSet</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
List<span style="color:#3d8fd1"><span style="color:#999999"><</span>Person<span style="color:#999999">></span></span> personList <span style="color:#ac9739">=</span> <span style="color:#6679cc">new</span> ArrayList<span style="color:#3d8fd1"><span style="color:#999999"><</span>Person<span style="color:#999999">></span></span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
personList<span style="color:#999999">.</span><span style="color:#3d8fd1">add</span><span style="color:#999999">(</span><span style="color:#6679cc">new</span> Person<span style="color:#999999">(</span><span style="color:#ac9739">"Tom"</span><span style="color:#999999">,</span> <span style="color:#ac9739">8900</span><span style="color:#999999">,</span> <span style="color:#ac9739">23</span><span style="color:#999999">,</span> <span style="color:#ac9739">"male"</span><span style="color:#999999">,</span> <span style="color:#ac9739">"New York"</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
personList<span style="color:#999999">.</span><span style="color:#3d8fd1">add</span><span style="color:#999999">(</span><span style="color:#6679cc">new</span> Person<span style="color:#999999">(</span><span style="color:#ac9739">"Jack"</span><span style="color:#999999">,</span> <span style="color:#ac9739">7000</span><span style="color:#999999">,</span> <span style="color:#ac9739">25</span><span style="color:#999999">,</span> <span style="color:#ac9739">"male"</span><span style="color:#999999">,</span> <span style="color:#ac9739">"Washington"</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
personList<span style="color:#999999">.</span><span style="color:#3d8fd1">add</span><span style="color:#999999">(</span><span style="color:#6679cc">new</span> Person<span style="color:#999999">(</span><span style="color:#ac9739">"Lily"</span><span style="color:#999999">,</span> <span style="color:#ac9739">7800</span><span style="color:#999999">,</span> <span style="color:#ac9739">21</span><span style="color:#999999">,</span> <span style="color:#ac9739">"female"</span><span style="color:#999999">,</span> <span style="color:#ac9739">"Washington"</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
personList<span style="color:#999999">.</span><span style="color:#3d8fd1">add</span><span style="color:#999999">(</span><span style="color:#6679cc">new</span> Person<span style="color:#999999">(</span><span style="color:#ac9739">"Anni"</span><span style="color:#999999">,</span> <span style="color:#ac9739">8200</span><span style="color:#999999">,</span> <span style="color:#ac9739">24</span><span style="color:#999999">,</span> <span style="color:#ac9739">"female"</span><span style="color:#999999">,</span> <span style="color:#ac9739">"New York"</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
Map<span style="color:#ac9739"><</span><span style="color:#ac9739">?</span><span style="color:#999999">,</span> Person<span style="color:#ac9739">></span> map <span style="color:#ac9739">=</span> personList<span style="color:#999999">.</span><span style="color:#3d8fd1">stream</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#3d8fd1">filter</span><span style="color:#999999">(</span>p <span style="color:#ac9739">-</span><span style="color:#ac9739">></span> p<span style="color:#999999">.</span><span style="color:#3d8fd1">getSalary</span><span style="color:#999999">(</span><span style="color:#999999">)</span> <span style="color:#ac9739">></span> <span style="color:#ac9739">8000</span><span style="color:#999999">)</span>
<span style="color:#999999">.</span><span style="color:#3d8fd1">collect</span><span style="color:#999999">(</span>Collectors<span style="color:#999999">.</span><span style="color:#3d8fd1">toMap</span><span style="color:#999999">(</span>Person<span style="color:#ac9739">:</span><span style="color:#ac9739">:</span>getName<span style="color:#999999">,</span> p <span style="color:#ac9739">-</span><span style="color:#ac9739">></span> p<span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#3d8fd1">println</span><span style="color:#999999">(</span><span style="color:#ac9739">"toList:"</span> <span style="color:#ac9739">+</span> listNew<span style="color:#999999">)</span><span style="color:#999999">;</span>
System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#3d8fd1">println</span><span style="color:#999999">(</span><span style="color:#ac9739">"toSet:"</span> <span style="color:#ac9739">+</span> set<span style="color:#999999">)</span><span style="color:#999999">;</span>
System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#3d8fd1">println</span><span style="color:#999999">(</span><span style="color:#ac9739">"toMap:"</span> <span style="color:#ac9739">+</span> map<span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#999999">}</span>
<span style="color:#999999">}</span>
</code></span>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
运行结果:
toList:[6, 4, 6, 6, 20]
toSet:[4, 20, 6]
toMap:{Tom=mutest.Person@5fd0d5ae, Anni=mutest.Person@2d98a335}
3.6.2 统计(count/averaging)
Collectors
提供了一系列用于数据统计的静态方法:
- 计数:
count
- 平均值:
averagingInt
、averagingLong
、averagingDouble
- 最值:
maxBy
、minBy
- 求和:
summingInt
、summingLong
、summingDouble
- 统计以上所有:
summarizingInt
、summarizingLong
、summarizingDouble
案例:统计员工人数、平均工资、工资总额、最高工资。
<span style="color:#000000"><code class="language-java"><span style="color:#6679cc">public</span> <span style="color:#6679cc">class</span> StreamTest <span style="color:#999999">{</span>
<span style="color:#6679cc">public</span> <span style="color:#6679cc">static</span> <span style="color:#6679cc">void</span> <span style="color:#3d8fd1">main</span><span style="color:#999999">(</span>String<span style="color:#999999">[</span><span style="color:#999999">]</span> args<span style="color:#999999">)</span> <span style="color:#999999">{</span>
List<span style="color:#3d8fd1"><span style="color:#999999"><</span>Person<span style="color:#999999">></span></span> personList <span style="color:#ac9739">=</span> <span style="color:#6679cc">new</span> ArrayList<span style="color:#3d8fd1"><span style="color:#999999"><</span>Person<span style="color:#999999">></span></span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
personList<span style="color:#999999">.</span><span style="color:#3d8fd1">add</span><span style="color:#999999">(</span><span style="color:#6679cc">new</span> Person<span style="color:#999999">(</span><span style="color:#ac9739">"Tom"</span><span style="color:#999999">,</span> <span style="color:#ac9739">8900</span><span style="color:#999999">,</span> <span style="color:#ac9739">23</span><span style="color:#999999">,</span> <span style="color:#ac9739">"male"</span><span style="color:#999999">,</span> <span style="color:#ac9739">"New York"</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
personList<span style="color:#999999">.</span><span style="color:#3d8fd1">add</span><span style="color:#999999">(</span><span style="color:#6679cc">new</span> Person<span style="color:#999999">(</span><span style="color:#ac9739">"Jack"</span><span style="color:#999999">,</span> <span style="color:#ac9739">7000</span><span style="color:#999999">,</span> <span style="color:#ac9739">25</span><span style="color:#999999">,</span> <span style="color:#ac9739">"male"</span><span style="color:#999999">,</span> <span style="color:#ac9739">"Washington"</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
personList<span style="color:#999999">.</span><span style="color:#3d8fd1">add</span><span style="color:#999999">(</span><span style="color:#6679cc">new</span> Person<span style="color:#999999">(</span><span style="color:#ac9739">"Lily"</span><span style="color:#999999">,</span> <span style="color:#ac9739">7800</span><span style="color:#999999">,</span> <span style="color:#ac9739">21</span><span style="color:#999999">,</span> <span style="color:#ac9739">"female"</span><span style="color:#999999">,</span> <span style="color:#ac9739">"Washington"</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#6b7394">// 求总数</span>
Long count <span style="color:#ac9739">=</span> personList<span style="color:#999999">.</span><span style="color:#3d8fd1">stream</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#3d8fd1">collect</span><span style="color:#999999">(</span>Collectors<span style="color:#999999">.</span><span style="color:#3d8fd1">counting</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#6b7394">// 求平均工资</span>
Double average <span style="color:#ac9739">=</span> personList<span style="color:#999999">.</span><span style="color:#3d8fd1">stream</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#3d8fd1">collect</span><span style="color:#999999">(</span>Collectors<span style="color:#999999">.</span><span style="color:#3d8fd1">averagingDouble</span><span style="color:#999999">(</span>Person<span style="color:#ac9739">:</span><span style="color:#ac9739">:</span>getSalary<span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#6b7394">// 求最高工资</span>
Optional<span style="color:#3d8fd1"><span style="color:#999999"><</span>Integer<span style="color:#999999">></span></span> max <span style="color:#ac9739">=</span> personList<span style="color:#999999">.</span><span style="color:#3d8fd1">stream</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#3d8fd1">map</span><span style="color:#999999">(</span>Person<span style="color:#ac9739">:</span><span style="color:#ac9739">:</span>getSalary<span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#3d8fd1">collect</span><span style="color:#999999">(</span>Collectors<span style="color:#999999">.</span><span style="color:#3d8fd1">maxBy</span><span style="color:#999999">(</span>Integer<span style="color:#ac9739">:</span><span style="color:#ac9739">:</span>compare<span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#6b7394">// 求工资之和</span>
Integer sum <span style="color:#ac9739">=</span> personList<span style="color:#999999">.</span><span style="color:#3d8fd1">stream</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#3d8fd1">collect</span><span style="color:#999999">(</span>Collectors<span style="color:#999999">.</span><span style="color:#3d8fd1">summingInt</span><span style="color:#999999">(</span>Person<span style="color:#ac9739">:</span><span style="color:#ac9739">:</span>getSalary<span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#6b7394">// 一次性统计所有信息</span>
DoubleSummaryStatistics collect <span style="color:#ac9739">=</span> personList<span style="color:#999999">.</span><span style="color:#3d8fd1">stream</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#3d8fd1">collect</span><span style="color:#999999">(</span>Collectors<span style="color:#999999">.</span><span style="color:#3d8fd1">summarizingDouble</span><span style="color:#999999">(</span>Person<span style="color:#ac9739">:</span><span style="color:#ac9739">:</span>getSalary<span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#3d8fd1">println</span><span style="color:#999999">(</span><span style="color:#ac9739">"员工总数:"</span> <span style="color:#ac9739">+</span> count<span style="color:#999999">)</span><span style="color:#999999">;</span>
System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#3d8fd1">println</span><span style="color:#999999">(</span><span style="color:#ac9739">"员工平均工资:"</span> <span style="color:#ac9739">+</span> average<span style="color:#999999">)</span><span style="color:#999999">;</span>
System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#3d8fd1">println</span><span style="color:#999999">(</span><span style="color:#ac9739">"员工工资总和:"</span> <span style="color:#ac9739">+</span> sum<span style="color:#999999">)</span><span style="color:#999999">;</span>
System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#3d8fd1">println</span><span style="color:#999999">(</span><span style="color:#ac9739">"员工工资所有统计:"</span> <span style="color:#ac9739">+</span> collect<span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#999999">}</span>
<span style="color:#999999">}</span>
</code></span>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
运行结果:
员工总数:3
员工平均工资:7900.0
员工工资总和:23700
员工工资所有统计:DoubleSummaryStatistics{count=3, sum=23700.000000,min=7000.000000, average=7900.000000, max=8900.000000}
3.6.3 分组(partitioningBy/groupingBy)
- 分区:将
stream
按条件分为两个Map
,比如员工按薪资是否高于8000分为两部分。 - 分组:将集合分为多个Map,比如员工按性别分组。有单级分组和多级分组。
案例:将员工按薪资是否高于8000分为两部分;将员工按性别和地区分组
<span style="color:#000000"><code class="language-java"><span style="color:#6679cc">public</span> <span style="color:#6679cc">class</span> StreamTest <span style="color:#999999">{</span>
<span style="color:#6679cc">public</span> <span style="color:#6679cc">static</span> <span style="color:#6679cc">void</span> <span style="color:#3d8fd1">main</span><span style="color:#999999">(</span>String<span style="color:#999999">[</span><span style="color:#999999">]</span> args<span style="color:#999999">)</span> <span style="color:#999999">{</span>
List<span style="color:#3d8fd1"><span style="color:#999999"><</span>Person<span style="color:#999999">></span></span> personList <span style="color:#ac9739">=</span> <span style="color:#6679cc">new</span> ArrayList<span style="color:#3d8fd1"><span style="color:#999999"><</span>Person<span style="color:#999999">></span></span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
personList<span style="color:#999999">.</span><span style="color:#3d8fd1">add</span><span style="color:#999999">(</span><span style="color:#6679cc">new</span> Person<span style="color:#999999">(</span><span style="color:#ac9739">"Tom"</span><span style="color:#999999">,</span> <span style="color:#ac9739">8900</span><span style="color:#999999">,</span> <span style="color:#ac9739">"male"</span><span style="color:#999999">,</span> <span style="color:#ac9739">"New York"</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
personList<span style="color:#999999">.</span><span style="color:#3d8fd1">add</span><span style="color:#999999">(</span><span style="color:#6679cc">new</span> Person<span style="color:#999999">(</span><span style="color:#ac9739">"Jack"</span><span style="color:#999999">,</span> <span style="color:#ac9739">7000</span><span style="color:#999999">,</span> <span style="color:#ac9739">"male"</span><span style="color:#999999">,</span> <span style="color:#ac9739">"Washington"</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
personList<span style="color:#999999">.</span><span style="color:#3d8fd1">add</span><span style="color:#999999">(</span><span style="color:#6679cc">new</span> Person<span style="color:#999999">(</span><span style="color:#ac9739">"Lily"</span><span style="color:#999999">,</span> <span style="color:#ac9739">7800</span><span style="color:#999999">,</span> <span style="color:#ac9739">"female"</span><span style="color:#999999">,</span> <span style="color:#ac9739">"Washington"</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
personList<span style="color:#999999">.</span><span style="color:#3d8fd1">add</span><span style="color:#999999">(</span><span style="color:#6679cc">new</span> Person<span style="color:#999999">(</span><span style="color:#ac9739">"Anni"</span><span style="color:#999999">,</span> <span style="color:#ac9739">8200</span><span style="color:#999999">,</span> <span style="color:#ac9739">"female"</span><span style="color:#999999">,</span> <span style="color:#ac9739">"New York"</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
personList<span style="color:#999999">.</span><span style="color:#3d8fd1">add</span><span style="color:#999999">(</span><span style="color:#6679cc">new</span> Person<span style="color:#999999">(</span><span style="color:#ac9739">"Owen"</span><span style="color:#999999">,</span> <span style="color:#ac9739">9500</span><span style="color:#999999">,</span> <span style="color:#ac9739">"male"</span><span style="color:#999999">,</span> <span style="color:#ac9739">"New York"</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
personList<span style="color:#999999">.</span><span style="color:#3d8fd1">add</span><span style="color:#999999">(</span><span style="color:#6679cc">new</span> Person<span style="color:#999999">(</span><span style="color:#ac9739">"Alisa"</span><span style="color:#999999">,</span> <span style="color:#ac9739">7900</span><span style="color:#999999">,</span> <span style="color:#ac9739">"female"</span><span style="color:#999999">,</span> <span style="color:#ac9739">"New York"</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#6b7394">// 将员工按薪资是否高于8000分组</span>
Map<span style="color:#ac9739"><</span>Boolean<span style="color:#999999">,</span> List<span style="color:#3d8fd1"><span style="color:#999999"><</span>Person<span style="color:#999999">></span></span><span style="color:#ac9739">></span> part <span style="color:#ac9739">=</span> personList<span style="color:#999999">.</span><span style="color:#3d8fd1">stream</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#3d8fd1">collect</span><span style="color:#999999">(</span>Collectors<span style="color:#999999">.</span><span style="color:#3d8fd1">partitioningBy</span><span style="color:#999999">(</span>x <span style="color:#ac9739">-</span><span style="color:#ac9739">></span> x<span style="color:#999999">.</span><span style="color:#3d8fd1">getSalary</span><span style="color:#999999">(</span><span style="color:#999999">)</span> <span style="color:#ac9739">></span> <span style="color:#ac9739">8000</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#6b7394">// 将员工按性别分组</span>
Map<span style="color:#ac9739"><</span>String<span style="color:#999999">,</span> List<span style="color:#3d8fd1"><span style="color:#999999"><</span>Person<span style="color:#999999">></span></span><span style="color:#ac9739">></span> group <span style="color:#ac9739">=</span> personList<span style="color:#999999">.</span><span style="color:#3d8fd1">stream</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#3d8fd1">collect</span><span style="color:#999999">(</span>Collectors<span style="color:#999999">.</span><span style="color:#3d8fd1">groupingBy</span><span style="color:#999999">(</span>Person<span style="color:#ac9739">:</span><span style="color:#ac9739">:</span>getSex<span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#6b7394">// 将员工先按性别分组,再按地区分组</span>
Map<span style="color:#ac9739"><</span>String<span style="color:#999999">,</span> Map<span style="color:#ac9739"><</span>String<span style="color:#999999">,</span> List<span style="color:#3d8fd1"><span style="color:#999999"><</span>Person<span style="color:#999999">></span></span><span style="color:#ac9739">>></span> group2 <span style="color:#ac9739">=</span> personList<span style="color:#999999">.</span><span style="color:#3d8fd1">stream</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#3d8fd1">collect</span><span style="color:#999999">(</span>Collectors<span style="color:#999999">.</span><span style="color:#3d8fd1">groupingBy</span><span style="color:#999999">(</span>Person<span style="color:#ac9739">:</span><span style="color:#ac9739">:</span>getSex<span style="color:#999999">,</span> Collectors<span style="color:#999999">.</span><span style="color:#3d8fd1">groupingBy</span><span style="color:#999999">(</span>Person<span style="color:#ac9739">:</span><span style="color:#ac9739">:</span>getArea<span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#3d8fd1">println</span><span style="color:#999999">(</span><span style="color:#ac9739">"员工按薪资是否大于8000分组情况:"</span> <span style="color:#ac9739">+</span> part<span style="color:#999999">)</span><span style="color:#999999">;</span>
System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#3d8fd1">println</span><span style="color:#999999">(</span><span style="color:#ac9739">"员工按性别分组情况:"</span> <span style="color:#ac9739">+</span> group<span style="color:#999999">)</span><span style="color:#999999">;</span>
System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#3d8fd1">println</span><span style="color:#999999">(</span><span style="color:#ac9739">"员工按性别、地区:"</span> <span style="color:#ac9739">+</span> group2<span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#999999">}</span>
<span style="color:#999999">}</span>
</code></span>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
输出结果:
<span style="color:#000000"><code class="language-java">员工按薪资是否大于<span style="color:#ac9739">8000</span>分组情况:<span style="color:#999999">{</span><span style="color:#c76b29">false</span><span style="color:#ac9739">=</span><span style="color:#999999">[</span>mutest<span style="color:#999999">.</span>Person<span style="color:#999999">@2d98a335</span><span style="color:#999999">,</span> mutest<span style="color:#999999">.</span>Person<span style="color:#999999">@16b98e56</span><span style="color:#999999">,</span> mutest<span style="color:#999999">.</span>Person<span style="color:#999999">@7ef20235</span><span style="color:#999999">]</span><span style="color:#999999">,</span> <span style="color:#c76b29">true</span><span style="color:#ac9739">=</span><span style="color:#999999">[</span>mutest<span style="color:#999999">.</span>Person<span style="color:#999999">@27d6c5e0</span><span style="color:#999999">,</span> mutest<span style="color:#999999">.</span>Person<span style="color:#999999">@4f3f5b24</span><span style="color:#999999">,</span> mutest<span style="color:#999999">.</span>Person<span style="color:#999999">@15aeb7ab</span><span style="color:#999999">]</span><span style="color:#999999">}</span>
员工按性别分组情况:<span style="color:#999999">{</span>female<span style="color:#ac9739">=</span><span style="color:#999999">[</span>mutest<span style="color:#999999">.</span>Person<span style="color:#999999">@16b98e56</span><span style="color:#999999">,</span> mutest<span style="color:#999999">.</span>Person<span style="color:#999999">@4f3f5b24</span><span style="color:#999999">,</span> mutest<span style="color:#999999">.</span>Person<span style="color:#999999">@7ef20235</span><span style="color:#999999">]</span><span style="color:#999999">,</span> male<span style="color:#ac9739">=</span><span style="color:#999999">[</span>mutest<span style="color:#999999">.</span>Person<span style="color:#999999">@27d6c5e0</span><span style="color:#999999">,</span> mutest<span style="color:#999999">.</span>Person<span style="color:#999999">@2d98a335</span><span style="color:#999999">,</span> mutest<span style="color:#999999">.</span>Person<span style="color:#999999">@15aeb7ab</span><span style="color:#999999">]</span><span style="color:#999999">}</span>
员工按性别、地区:<span style="color:#999999">{</span>female<span style="color:#ac9739">=</span><span style="color:#999999">{</span>New York<span style="color:#ac9739">=</span><span style="color:#999999">[</span>mutest<span style="color:#999999">.</span>Person<span style="color:#999999">@4f3f5b24</span><span style="color:#999999">,</span> mutest<span style="color:#999999">.</span>Person<span style="color:#999999">@7ef20235</span><span style="color:#999999">]</span><span style="color:#999999">,</span> Washington<span style="color:#ac9739">=</span><span style="color:#999999">[</span>mutest<span style="color:#999999">.</span>Person<span style="color:#999999">@16b98e56</span><span style="color:#999999">]</span><span style="color:#999999">}</span><span style="color:#999999">,</span> male<span style="color:#ac9739">=</span><span style="color:#999999">{</span>New York<span style="color:#ac9739">=</span><span style="color:#999999">[</span>mutest<span style="color:#999999">.</span>Person<span style="color:#999999">@27d6c5e0</span><span style="color:#999999">,</span> mutest<span style="color:#999999">.</span>Person<span style="color:#999999">@15aeb7ab</span><span style="color:#999999">]</span><span style="color:#999999">,</span> Washington<span style="color:#ac9739">=</span><span style="color:#999999">[</span>mutest<span style="color:#999999">.</span>Person<span style="color:#999999">@2d98a335</span><span style="color:#999999">]</span><span style="color:#999999">}</span><span style="color:#999999">}</span>
</code></span>
- 1
- 2
- 3
3.6.4 接合(joining)
joining
可以将stream中的元素用特定的连接符(没有的话,则直接连接)连接成一个字符串。
<span style="color:#000000"><code class="language-java"><span style="color:#6679cc">public</span> <span style="color:#6679cc">class</span> StreamTest <span style="color:#999999">{</span>
<span style="color:#6679cc">public</span> <span style="color:#6679cc">static</span> <span style="color:#6679cc">void</span> <span style="color:#3d8fd1">main</span><span style="color:#999999">(</span>String<span style="color:#999999">[</span><span style="color:#999999">]</span> args<span style="color:#999999">)</span> <span style="color:#999999">{</span>
List<span style="color:#3d8fd1"><span style="color:#999999"><</span>Person<span style="color:#999999">></span></span> personList <span style="color:#ac9739">=</span> <span style="color:#6679cc">new</span> ArrayList<span style="color:#3d8fd1"><span style="color:#999999"><</span>Person<span style="color:#999999">></span></span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
personList<span style="color:#999999">.</span><span style="color:#3d8fd1">add</span><span style="color:#999999">(</span><span style="color:#6679cc">new</span> Person<span style="color:#999999">(</span><span style="color:#ac9739">"Tom"</span><span style="color:#999999">,</span> <span style="color:#ac9739">8900</span><span style="color:#999999">,</span> <span style="color:#ac9739">23</span><span style="color:#999999">,</span> <span style="color:#ac9739">"male"</span><span style="color:#999999">,</span> <span style="color:#ac9739">"New York"</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
personList<span style="color:#999999">.</span><span style="color:#3d8fd1">add</span><span style="color:#999999">(</span><span style="color:#6679cc">new</span> Person<span style="color:#999999">(</span><span style="color:#ac9739">"Jack"</span><span style="color:#999999">,</span> <span style="color:#ac9739">7000</span><span style="color:#999999">,</span> <span style="color:#ac9739">25</span><span style="color:#999999">,</span> <span style="color:#ac9739">"male"</span><span style="color:#999999">,</span> <span style="color:#ac9739">"Washington"</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
personList<span style="color:#999999">.</span><span style="color:#3d8fd1">add</span><span style="color:#999999">(</span><span style="color:#6679cc">new</span> Person<span style="color:#999999">(</span><span style="color:#ac9739">"Lily"</span><span style="color:#999999">,</span> <span style="color:#ac9739">7800</span><span style="color:#999999">,</span> <span style="color:#ac9739">21</span><span style="color:#999999">,</span> <span style="color:#ac9739">"female"</span><span style="color:#999999">,</span> <span style="color:#ac9739">"Washington"</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
String names <span style="color:#ac9739">=</span> personList<span style="color:#999999">.</span><span style="color:#3d8fd1">stream</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#3d8fd1">map</span><span style="color:#999999">(</span>p <span style="color:#ac9739">-</span><span style="color:#ac9739">></span> p<span style="color:#999999">.</span><span style="color:#3d8fd1">getName</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#3d8fd1">collect</span><span style="color:#999999">(</span>Collectors<span style="color:#999999">.</span><span style="color:#3d8fd1">joining</span><span style="color:#999999">(</span><span style="color:#ac9739">","</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#3d8fd1">println</span><span style="color:#999999">(</span><span style="color:#ac9739">"所有员工的姓名:"</span> <span style="color:#ac9739">+</span> names<span style="color:#999999">)</span><span style="color:#999999">;</span>
List<span style="color:#3d8fd1"><span style="color:#999999"><</span>String<span style="color:#999999">></span></span> list <span style="color:#ac9739">=</span> Arrays<span style="color:#999999">.</span><span style="color:#3d8fd1">asList</span><span style="color:#999999">(</span><span style="color:#ac9739">"A"</span><span style="color:#999999">,</span> <span style="color:#ac9739">"B"</span><span style="color:#999999">,</span> <span style="color:#ac9739">"C"</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
String string <span style="color:#ac9739">=</span> list<span style="color:#999999">.</span><span style="color:#3d8fd1">stream</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#3d8fd1">collect</span><span style="color:#999999">(</span>Collectors<span style="color:#999999">.</span><span style="color:#3d8fd1">joining</span><span style="color:#999999">(</span><span style="color:#ac9739">"-"</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#3d8fd1">println</span><span style="color:#999999">(</span><span style="color:#ac9739">"拼接后的字符串:"</span> <span style="color:#ac9739">+</span> string<span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#999999">}</span>
<span style="color:#999999">}</span>
</code></span>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
运行结果:
所有员工的姓名:Tom,Jack,Lily
拼接后的字符串:A-B-C
3.6.5 归约(reducing)
Collectors
类提供的reducing
方法,相比于stream
本身的reduce
方法,增加了对自定义归约的支持。
<span style="color:#000000"><code class="language-java"><span style="color:#6679cc">public</span> <span style="color:#6679cc">class</span> StreamTest <span style="color:#999999">{</span>
<span style="color:#6679cc">public</span> <span style="color:#6679cc">static</span> <span style="color:#6679cc">void</span> <span style="color:#3d8fd1">main</span><span style="color:#999999">(</span>String<span style="color:#999999">[</span><span style="color:#999999">]</span> args<span style="color:#999999">)</span> <span style="color:#999999">{</span>
List<span style="color:#3d8fd1"><span style="color:#999999"><</span>Person<span style="color:#999999">></span></span> personList <span style="color:#ac9739">=</span> <span style="color:#6679cc">new</span> ArrayList<span style="color:#3d8fd1"><span style="color:#999999"><</span>Person<span style="color:#999999">></span></span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
personList<span style="color:#999999">.</span><span style="color:#3d8fd1">add</span><span style="color:#999999">(</span><span style="color:#6679cc">new</span> Person<span style="color:#999999">(</span><span style="color:#ac9739">"Tom"</span><span style="color:#999999">,</span> <span style="color:#ac9739">8900</span><span style="color:#999999">,</span> <span style="color:#ac9739">23</span><span style="color:#999999">,</span> <span style="color:#ac9739">"male"</span><span style="color:#999999">,</span> <span style="color:#ac9739">"New York"</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
personList<span style="color:#999999">.</span><span style="color:#3d8fd1">add</span><span style="color:#999999">(</span><span style="color:#6679cc">new</span> Person<span style="color:#999999">(</span><span style="color:#ac9739">"Jack"</span><span style="color:#999999">,</span> <span style="color:#ac9739">7000</span><span style="color:#999999">,</span> <span style="color:#ac9739">25</span><span style="color:#999999">,</span> <span style="color:#ac9739">"male"</span><span style="color:#999999">,</span> <span style="color:#ac9739">"Washington"</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
personList<span style="color:#999999">.</span><span style="color:#3d8fd1">add</span><span style="color:#999999">(</span><span style="color:#6679cc">new</span> Person<span style="color:#999999">(</span><span style="color:#ac9739">"Lily"</span><span style="color:#999999">,</span> <span style="color:#ac9739">7800</span><span style="color:#999999">,</span> <span style="color:#ac9739">21</span><span style="color:#999999">,</span> <span style="color:#ac9739">"female"</span><span style="color:#999999">,</span> <span style="color:#ac9739">"Washington"</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#6b7394">// 每个员工减去起征点后的薪资之和(这个例子并不严谨,但一时没想到好的例子)</span>
Integer sum <span style="color:#ac9739">=</span> personList<span style="color:#999999">.</span><span style="color:#3d8fd1">stream</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#3d8fd1">collect</span><span style="color:#999999">(</span>Collectors<span style="color:#999999">.</span><span style="color:#3d8fd1">reducing</span><span style="color:#999999">(</span><span style="color:#ac9739">0</span><span style="color:#999999">,</span> Person<span style="color:#ac9739">:</span><span style="color:#ac9739">:</span>getSalary<span style="color:#999999">,</span> <span style="color:#999999">(</span>i<span style="color:#999999">,</span> j<span style="color:#999999">)</span> <span style="color:#ac9739">-</span><span style="color:#ac9739">></span> <span style="color:#999999">(</span>i <span style="color:#ac9739">+</span> j <span style="color:#ac9739">-</span> <span style="color:#ac9739">5000</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#3d8fd1">println</span><span style="color:#999999">(</span><span style="color:#ac9739">"员工扣税薪资总和:"</span> <span style="color:#ac9739">+</span> sum<span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#6b7394">// stream的reduce</span>
Optional<span style="color:#3d8fd1"><span style="color:#999999"><</span>Integer<span style="color:#999999">></span></span> sum2 <span style="color:#ac9739">=</span> personList<span style="color:#999999">.</span><span style="color:#3d8fd1">stream</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#3d8fd1">map</span><span style="color:#999999">(</span>Person<span style="color:#ac9739">:</span><span style="color:#ac9739">:</span>getSalary<span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#3d8fd1">reduce</span><span style="color:#999999">(</span>Integer<span style="color:#ac9739">:</span><span style="color:#ac9739">:</span>sum<span style="color:#999999">)</span><span style="color:#999999">;</span>
System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#3d8fd1">println</span><span style="color:#999999">(</span><span style="color:#ac9739">"员工薪资总和:"</span> <span style="color:#ac9739">+</span> sum2<span style="color:#999999">.</span><span style="color:#3d8fd1">get</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#999999">}</span>
<span style="color:#999999">}</span>
</code></span>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
运行结果:
员工扣税薪资总和:8700
员工薪资总和:23700
3.7 排序(sorted)
sorted,中间操作。有两种排序:
- sorted():自然排序,流中元素需实现Comparable接口
- sorted(Comparator com):Comparator排序器自定义排序
案例:将员工按工资由高到低(工资一样则按年龄由大到小)排序
<span style="color:#000000"><code class="language-java"><span style="color:#6679cc">public</span> <span style="color:#6679cc">class</span> StreamTest <span style="color:#999999">{</span>
<span style="color:#6679cc">public</span> <span style="color:#6679cc">static</span> <span style="color:#6679cc">void</span> <span style="color:#3d8fd1">main</span><span style="color:#999999">(</span>String<span style="color:#999999">[</span><span style="color:#999999">]</span> args<span style="color:#999999">)</span> <span style="color:#999999">{</span>
List<span style="color:#3d8fd1"><span style="color:#999999"><</span>Person<span style="color:#999999">></span></span> personList <span style="color:#ac9739">=</span> <span style="color:#6679cc">new</span> ArrayList<span style="color:#3d8fd1"><span style="color:#999999"><</span>Person<span style="color:#999999">></span></span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
personList<span style="color:#999999">.</span><span style="color:#3d8fd1">add</span><span style="color:#999999">(</span><span style="color:#6679cc">new</span> Person<span style="color:#999999">(</span><span style="color:#ac9739">"Sherry"</span><span style="color:#999999">,</span> <span style="color:#ac9739">9000</span><span style="color:#999999">,</span> <span style="color:#ac9739">24</span><span style="color:#999999">,</span> <span style="color:#ac9739">"female"</span><span style="color:#999999">,</span> <span style="color:#ac9739">"New York"</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
personList<span style="color:#999999">.</span><span style="color:#3d8fd1">add</span><span style="color:#999999">(</span><span style="color:#6679cc">new</span> Person<span style="color:#999999">(</span><span style="color:#ac9739">"Tom"</span><span style="color:#999999">,</span> <span style="color:#ac9739">8900</span><span style="color:#999999">,</span> <span style="color:#ac9739">22</span><span style="color:#999999">,</span> <span style="color:#ac9739">"male"</span><span style="color:#999999">,</span> <span style="color:#ac9739">"Washington"</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
personList<span style="color:#999999">.</span><span style="color:#3d8fd1">add</span><span style="color:#999999">(</span><span style="color:#6679cc">new</span> Person<span style="color:#999999">(</span><span style="color:#ac9739">"Jack"</span><span style="color:#999999">,</span> <span style="color:#ac9739">9000</span><span style="color:#999999">,</span> <span style="color:#ac9739">25</span><span style="color:#999999">,</span> <span style="color:#ac9739">"male"</span><span style="color:#999999">,</span> <span style="color:#ac9739">"Washington"</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
personList<span style="color:#999999">.</span><span style="color:#3d8fd1">add</span><span style="color:#999999">(</span><span style="color:#6679cc">new</span> Person<span style="color:#999999">(</span><span style="color:#ac9739">"Lily"</span><span style="color:#999999">,</span> <span style="color:#ac9739">8800</span><span style="color:#999999">,</span> <span style="color:#ac9739">26</span><span style="color:#999999">,</span> <span style="color:#ac9739">"male"</span><span style="color:#999999">,</span> <span style="color:#ac9739">"New York"</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
personList<span style="color:#999999">.</span><span style="color:#3d8fd1">add</span><span style="color:#999999">(</span><span style="color:#6679cc">new</span> Person<span style="color:#999999">(</span><span style="color:#ac9739">"Alisa"</span><span style="color:#999999">,</span> <span style="color:#ac9739">9000</span><span style="color:#999999">,</span> <span style="color:#ac9739">26</span><span style="color:#999999">,</span> <span style="color:#ac9739">"female"</span><span style="color:#999999">,</span> <span style="color:#ac9739">"New York"</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#6b7394">// 按工资升序排序(自然排序)</span>
List<span style="color:#3d8fd1"><span style="color:#999999"><</span>String<span style="color:#999999">></span></span> newList <span style="color:#ac9739">=</span> personList<span style="color:#999999">.</span><span style="color:#3d8fd1">stream</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#3d8fd1">sorted</span><span style="color:#999999">(</span>Comparator<span style="color:#999999">.</span><span style="color:#3d8fd1">comparing</span><span style="color:#999999">(</span>Person<span style="color:#ac9739">:</span><span style="color:#ac9739">:</span>getSalary<span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#3d8fd1">map</span><span style="color:#999999">(</span>Person<span style="color:#ac9739">:</span><span style="color:#ac9739">:</span>getName<span style="color:#999999">)</span>
<span style="color:#999999">.</span><span style="color:#3d8fd1">collect</span><span style="color:#999999">(</span>Collectors<span style="color:#999999">.</span><span style="color:#3d8fd1">toList</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#6b7394">// 按工资倒序排序</span>
List<span style="color:#3d8fd1"><span style="color:#999999"><</span>String<span style="color:#999999">></span></span> newList2 <span style="color:#ac9739">=</span> personList<span style="color:#999999">.</span><span style="color:#3d8fd1">stream</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#3d8fd1">sorted</span><span style="color:#999999">(</span>Comparator<span style="color:#999999">.</span><span style="color:#3d8fd1">comparing</span><span style="color:#999999">(</span>Person<span style="color:#ac9739">:</span><span style="color:#ac9739">:</span>getSalary<span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#3d8fd1">reversed</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">)</span>
<span style="color:#999999">.</span><span style="color:#3d8fd1">map</span><span style="color:#999999">(</span>Person<span style="color:#ac9739">:</span><span style="color:#ac9739">:</span>getName<span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#3d8fd1">collect</span><span style="color:#999999">(</span>Collectors<span style="color:#999999">.</span><span style="color:#3d8fd1">toList</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#6b7394">// 先按工资再按年龄升序排序</span>
List<span style="color:#3d8fd1"><span style="color:#999999"><</span>String<span style="color:#999999">></span></span> newList3 <span style="color:#ac9739">=</span> personList<span style="color:#999999">.</span><span style="color:#3d8fd1">stream</span><span style="color:#999999">(</span><span style="color:#999999">)</span>
<span style="color:#999999">.</span><span style="color:#3d8fd1">sorted</span><span style="color:#999999">(</span>Comparator<span style="color:#999999">.</span><span style="color:#3d8fd1">comparing</span><span style="color:#999999">(</span>Person<span style="color:#ac9739">:</span><span style="color:#ac9739">:</span>getSalary<span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#3d8fd1">thenComparing</span><span style="color:#999999">(</span>Person<span style="color:#ac9739">:</span><span style="color:#ac9739">:</span>getAge<span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#3d8fd1">map</span><span style="color:#999999">(</span>Person<span style="color:#ac9739">:</span><span style="color:#ac9739">:</span>getName<span style="color:#999999">)</span>
<span style="color:#999999">.</span><span style="color:#3d8fd1">collect</span><span style="color:#999999">(</span>Collectors<span style="color:#999999">.</span><span style="color:#3d8fd1">toList</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#6b7394">// 先按工资再按年龄自定义排序(降序)</span>
List<span style="color:#3d8fd1"><span style="color:#999999"><</span>String<span style="color:#999999">></span></span> newList4 <span style="color:#ac9739">=</span> personList<span style="color:#999999">.</span><span style="color:#3d8fd1">stream</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#3d8fd1">sorted</span><span style="color:#999999">(</span><span style="color:#999999">(</span>p1<span style="color:#999999">,</span> p2<span style="color:#999999">)</span> <span style="color:#ac9739">-</span><span style="color:#ac9739">></span> <span style="color:#999999">{</span>
<span style="color:#6679cc">if</span> <span style="color:#999999">(</span>p1<span style="color:#999999">.</span><span style="color:#3d8fd1">getSalary</span><span style="color:#999999">(</span><span style="color:#999999">)</span> <span style="color:#ac9739">==</span> p2<span style="color:#999999">.</span><span style="color:#3d8fd1">getSalary</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">)</span> <span style="color:#999999">{</span>
<span style="color:#6679cc">return</span> p2<span style="color:#999999">.</span><span style="color:#3d8fd1">getAge</span><span style="color:#999999">(</span><span style="color:#999999">)</span> <span style="color:#ac9739">-</span> p1<span style="color:#999999">.</span><span style="color:#3d8fd1">getAge</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#999999">}</span> <span style="color:#6679cc">else</span> <span style="color:#999999">{</span>
<span style="color:#6679cc">return</span> p2<span style="color:#999999">.</span><span style="color:#3d8fd1">getSalary</span><span style="color:#999999">(</span><span style="color:#999999">)</span> <span style="color:#ac9739">-</span> p1<span style="color:#999999">.</span><span style="color:#3d8fd1">getSalary</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#999999">}</span>
<span style="color:#999999">}</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#3d8fd1">map</span><span style="color:#999999">(</span>Person<span style="color:#ac9739">:</span><span style="color:#ac9739">:</span>getName<span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#3d8fd1">collect</span><span style="color:#999999">(</span>Collectors<span style="color:#999999">.</span><span style="color:#3d8fd1">toList</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#3d8fd1">println</span><span style="color:#999999">(</span><span style="color:#ac9739">"按工资升序排序:"</span> <span style="color:#ac9739">+</span> newList<span style="color:#999999">)</span><span style="color:#999999">;</span>
System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#3d8fd1">println</span><span style="color:#999999">(</span><span style="color:#ac9739">"按工资降序排序:"</span> <span style="color:#ac9739">+</span> newList2<span style="color:#999999">)</span><span style="color:#999999">;</span>
System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#3d8fd1">println</span><span style="color:#999999">(</span><span style="color:#ac9739">"先按工资再按年龄升序排序:"</span> <span style="color:#ac9739">+</span> newList3<span style="color:#999999">)</span><span style="color:#999999">;</span>
System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#3d8fd1">println</span><span style="color:#999999">(</span><span style="color:#ac9739">"先按工资再按年龄自定义降序排序:"</span> <span style="color:#ac9739">+</span> newList4<span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#999999">}</span>
<span style="color:#999999">}</span>
</code></span>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
运行结果:
按工资自然排序:[Lily, Tom, Sherry, Jack, Alisa]
按工资降序排序:[Sherry, Jack, Alisa,Tom, Lily]
先按工资再按年龄自然排序:[Sherry, Jack, Alisa, Tom, Lily]
先按工资再按年龄自定义降序排序:[Alisa, Jack, Sherry, Tom, Lily]
3.8 提取/组合
流也可以进行合并、去重、限制、跳过等操作。
<span style="color:#000000"><code class="language-java"><span style="color:#6679cc">public</span> <span style="color:#6679cc">class</span> StreamTest <span style="color:#999999">{</span>
<span style="color:#6679cc">public</span> <span style="color:#6679cc">static</span> <span style="color:#6679cc">void</span> <span style="color:#3d8fd1">main</span><span style="color:#999999">(</span>String<span style="color:#999999">[</span><span style="color:#999999">]</span> args<span style="color:#999999">)</span> <span style="color:#999999">{</span>
String<span style="color:#999999">[</span><span style="color:#999999">]</span> arr1 <span style="color:#ac9739">=</span> <span style="color:#999999">{</span> <span style="color:#ac9739">"a"</span><span style="color:#999999">,</span> <span style="color:#ac9739">"b"</span><span style="color:#999999">,</span> <span style="color:#ac9739">"c"</span><span style="color:#999999">,</span> <span style="color:#ac9739">"d"</span> <span style="color:#999999">}</span><span style="color:#999999">;</span>
String<span style="color:#999999">[</span><span style="color:#999999">]</span> arr2 <span style="color:#ac9739">=</span> <span style="color:#999999">{</span> <span style="color:#ac9739">"d"</span><span style="color:#999999">,</span> <span style="color:#ac9739">"e"</span><span style="color:#999999">,</span> <span style="color:#ac9739">"f"</span><span style="color:#999999">,</span> <span style="color:#ac9739">"g"</span> <span style="color:#999999">}</span><span style="color:#999999">;</span>
Stream<span style="color:#3d8fd1"><span style="color:#999999"><</span>String<span style="color:#999999">></span></span> stream1 <span style="color:#ac9739">=</span> Stream<span style="color:#999999">.</span><span style="color:#3d8fd1">of</span><span style="color:#999999">(</span>arr1<span style="color:#999999">)</span><span style="color:#999999">;</span>
Stream<span style="color:#3d8fd1"><span style="color:#999999"><</span>String<span style="color:#999999">></span></span> stream2 <span style="color:#ac9739">=</span> Stream<span style="color:#999999">.</span><span style="color:#3d8fd1">of</span><span style="color:#999999">(</span>arr2<span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#6b7394">// concat:合并两个流 distinct:去重</span>
List<span style="color:#3d8fd1"><span style="color:#999999"><</span>String<span style="color:#999999">></span></span> newList <span style="color:#ac9739">=</span> Stream<span style="color:#999999">.</span><span style="color:#3d8fd1">concat</span><span style="color:#999999">(</span>stream1<span style="color:#999999">,</span> stream2<span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#3d8fd1">distinct</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#3d8fd1">collect</span><span style="color:#999999">(</span>Collectors<span style="color:#999999">.</span><span style="color:#3d8fd1">toList</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#6b7394">// limit:限制从流中获得前n个数据</span>
List<span style="color:#3d8fd1"><span style="color:#999999"><</span>Integer<span style="color:#999999">></span></span> collect <span style="color:#ac9739">=</span> Stream<span style="color:#999999">.</span><span style="color:#3d8fd1">iterate</span><span style="color:#999999">(</span><span style="color:#ac9739">1</span><span style="color:#999999">,</span> x <span style="color:#ac9739">-</span><span style="color:#ac9739">></span> x <span style="color:#ac9739">+</span> <span style="color:#ac9739">2</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#3d8fd1">limit</span><span style="color:#999999">(</span><span style="color:#ac9739">10</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#3d8fd1">collect</span><span style="color:#999999">(</span>Collectors<span style="color:#999999">.</span><span style="color:#3d8fd1">toList</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#6b7394">// skip:跳过前n个数据</span>
List<span style="color:#3d8fd1"><span style="color:#999999"><</span>Integer<span style="color:#999999">></span></span> collect2 <span style="color:#ac9739">=</span> Stream<span style="color:#999999">.</span><span style="color:#3d8fd1">iterate</span><span style="color:#999999">(</span><span style="color:#ac9739">1</span><span style="color:#999999">,</span> x <span style="color:#ac9739">-</span><span style="color:#ac9739">></span> x <span style="color:#ac9739">+</span> <span style="color:#ac9739">2</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#3d8fd1">skip</span><span style="color:#999999">(</span><span style="color:#ac9739">1</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#3d8fd1">limit</span><span style="color:#999999">(</span><span style="color:#ac9739">5</span><span style="color:#999999">)</span><span style="color:#999999">.</span><span style="color:#3d8fd1">collect</span><span style="color:#999999">(</span>Collectors<span style="color:#999999">.</span><span style="color:#3d8fd1">toList</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#3d8fd1">println</span><span style="color:#999999">(</span><span style="color:#ac9739">"流合并:"</span> <span style="color:#ac9739">+</span> newList<span style="color:#999999">)</span><span style="color:#999999">;</span>
System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#3d8fd1">println</span><span style="color:#999999">(</span><span style="color:#ac9739">"limit:"</span> <span style="color:#ac9739">+</span> collect<span style="color:#999999">)</span><span style="color:#999999">;</span>
System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#3d8fd1">println</span><span style="color:#999999">(</span><span style="color:#ac9739">"skip:"</span> <span style="color:#ac9739">+</span> collect2<span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#999999">}</span>
<span style="color:#999999">}</span>
</code></span>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
运行结果:
流合并:[a, b, c, d, e, f, g]
limit:[1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
skip:[3, 5, 7, 9, 11]