Java8新特性-Stream - 图1
先贴上几个案例,水平高超的同学可以挑战一下:

  1. 从员工集合中筛选出salary大于8000的员工,并放置到新的集合里。
  2. 统计员工的最高薪资、平均薪资、薪资之和。
  3. 将员工按薪资从高到低排序,同样薪资者年龄小者在前。
  4. 将员工按性别分类,将员工按性别和地区分类,将员工按薪资是否高于8000分为两部分。

用传统的迭代处理也不是很难,但代码就显得冗余了,跟Stream相比高下立判。

Stream概述

Java 8 是一个非常成功的版本,这个版本新增的Stream,配合同版本出现的 Lambda ,给我们操作集合(Collection)提供了极大的便利。
那么什么是Stream

Stream将要处理的元素集合看作一种流,在流的过程中,借助Stream API对流中的元素进行操作,比如:筛选、排序、聚合等。

Stream可以由数组或集合创建,对流的操作分为两种:

  1. 中间操作,每次返回一个新的流,可以有多个。
  2. 终端操作,每个流只能进行一次终端操作,终端操作结束后流无法再次使用。终端操作会产生一个新的集合或值。

Java8新特性-Stream - 图2
另外,Stream有几个特性:

  1. stream不存储数据,而是按照特定的规则对数据进行计算,一般会输出结果。
  2. stream不会改变数据源,通常情况下会产生一个新的集合或一个值。
  3. stream具有延迟执行特性,只有调用终端操作时,中间操作才会执行。

    Stream的创建

    Stream可以通过集合数组创建。
    (1)通过java.util.Collection下的 stream() 方法和 parallelStream() 用集合创建流: ```java List list = new ArrayList<>(); Stream stream = list.stream(); //获取一个顺序流 Stream parallelStream = list.parallelStream(); //获取一个并行流
  1. 2)使用`java.util.Arrays.stream(T[] array)`方法用数组创建流
  2. ```java
  3. Integer[] nums = new Integer[10];
  4. Stream<Integer> stream = Arrays.stream(nums);

(3)使用Stream的静态方法:of()、iterate()、generate()

  1. Stream<Integer> stream = Stream.of(1,2,3,4,5,6);
  2. Stream<Integer> stream2 = Stream.iterate(0, (x) -> x + 2).limit(6);
  3. stream2.forEach(System.out::println); // 0 2 4 6 8 10
  4. Stream<Double> stream3 = Stream.generate(Math::random).limit(2);
  5. stream3.forEach(System.out::println);

(4)使用 BufferedReader.lines() 方法,将每行内容转成流

  1. BufferedReader reader = new BufferedReader(new FileReader("F:\\test_stream.txt"));
  2. Stream<String> lineStream = reader.lines();
  3. lineStream.forEach(System.out::println);

(5)使用 Pattern.splitAsStream() 方法,将字符串分隔成流

  1. Pattern pattern = Pattern.compile(",");
  2. Stream<String> stringStream = pattern.splitAsStream("a,b,c,d");
  3. stringStream.forEach(System.out::println);

stream 和 parallelStream 的区分

streamparallelStream的简单区分:
stream是顺序流,由主线程按顺序对流执行操作,而parallelStream是并行流,内部以多线程并行执行的方式对流进行操作,但前提是流中的数据处理没有顺序要求。
例如筛选集合中的奇数,两者的处理不同之处:
Java8新特性-Stream - 图3
如果流中的数据量足够大,并行流可以加快处速度。
除了直接创建并行流,还可以通过parallel()把顺序流转换成并行流:

  1. Optional<Integer> findFirst = list.stream()
  2. .parallel()
  3. .filter(x->x>6)
  4. .findFirst();

Stream的使用

流的中间操作

3.1 遍历/匹配(foreach/find/match)

Stream也是支持类似集合的遍历和匹配元素的,只是Stream中的元素是以Optional类型存在的。Stream的遍历、匹配非常简单。
Java8新特性-Stream - 图4
// import已省略,请自行添加,后面代码亦是
public class StreamTest {
public static void main(String[] args) {
List list = Arrays.asList(7, 6, 9, 3, 8, 2, 1);

  1. <span class="token comment">// 遍历输出符合条件的元素</span>
  2. list<span class="token punctuation">.</span><span class="token function">stream</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">filter</span><span class="token punctuation">(</span>x <span class="token operator">-</span><span class="token operator">&gt;</span> x <span class="token operator">&gt;</span> <span class="token number">6</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">forEach</span><span class="token punctuation">(</span>System<span class="token punctuation">.</span>out<span class="token operator">:</span><span class="token operator">:</span>println<span class="token punctuation">)</span><span class="token punctuation">;</span>
  3. <span class="token comment">// 匹配第一个</span>
  4. Optional<span class="token generics function"><span class="token punctuation">&lt;</span>Integer<span class="token punctuation">&gt;</span></span> findFirst <span class="token operator">=</span> list<span class="token punctuation">.</span><span class="token function">stream</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">filter</span><span class="token punctuation">(</span>x <span class="token operator">-</span><span class="token operator">&gt;</span> x <span class="token operator">&gt;</span> <span class="token number">6</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">findFirst</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  5. <span class="token comment">// 匹配任意(适用于并行流)</span>
  6. Optional<span class="token generics function"><span class="token punctuation">&lt;</span>Integer<span class="token punctuation">&gt;</span></span> findAny <span class="token operator">=</span> list<span class="token punctuation">.</span><span class="token function">parallelStream</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">filter</span><span class="token punctuation">(</span>x <span class="token operator">-</span><span class="token operator">&gt;</span> x <span class="token operator">&gt;</span> <span class="token number">6</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">findAny</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  7. <span class="token comment">// 是否包含符合特定条件的元素</span>
  8. <span class="token keyword">boolean</span> anyMatch <span class="token operator">=</span> list<span class="token punctuation">.</span><span class="token function">stream</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">anyMatch</span><span class="token punctuation">(</span>x <span class="token operator">-</span><span class="token operator">&gt;</span> x <span class="token operator">&lt;</span> <span class="token number">6</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  9. System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"匹配第一个值:"</span> <span class="token operator">+</span> findFirst<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  10. System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"匹配任意一个值:"</span> <span class="token operator">+</span> findAny<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  11. System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"是否存在大于6的值:"</span> <span class="token operator">+</span> anyMatch<span class="token punctuation">)</span><span class="token punctuation">;</span>
  12. <span class="token punctuation">}</span>

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

    3.2 筛选(filter)

    筛选,是按照一定的规则校验流中的元素,将符合条件的元素提取到新的流中的操作。
    Java8新特性-Stream - 图5
    案例一:筛选出Integer集合中大于7的元素,并打印出来
    publicclassStreamTest{publicstaticvoidmain([]){=.asList(6,7,3,8,1,2,9);=.stream();.filter(->>7).forEach(.::);}}

    1. String args
    2. List list Arrays
    3. Stream stream list
    4. streamx x Systemoutprintln
  • 1

  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

预期结果:

8 9

案例二: 筛选员工中工资高于8000的人,并形成新的集合。 形成新集合依赖collect(收集),后文有详细介绍。
publicclassStreamTest{publicstaticvoidmain([]){=newArrayList();.add(newPerson(“Tom”,8900,23,”male”,”New York”));.add(newPerson(“Jack”,7000,25,”male”,”Washington”));.add(newPerson(“Lily”,7800,21,”female”,”Washington”));.add(newPerson(“Anni”,8200,24,”female”,”New York”));.add(newPerson(“Owen”,9500,25,”male”,”New York”));.add(newPerson(“Alisa”,7900,26,”female”,”New York”));

  1. List<span class="token generics function"><span class="token punctuation">&lt;</span>String<span class="token punctuation">&gt;</span></span> fiterList <span class="token operator">=</span> personList<span class="token punctuation">.</span><span class="token function">stream</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">filter</span><span class="token punctuation">(</span>x <span class="token operator">-</span><span class="token operator">&gt;</span> x<span class="token punctuation">.</span><span class="token function">getSalary</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">&gt;</span> <span class="token number">8000</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">map</span><span class="token punctuation">(</span>Person<span class="token operator">:</span><span class="token operator">:</span>getName<span class="token punctuation">)</span>
  2. <span class="token punctuation">.</span><span class="token function">collect</span><span class="token punctuation">(</span>Collectors<span class="token punctuation">.</span><span class="token function">toList</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  3. System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"高于8000的员工姓名:"</span> <span class="token operator">+</span> fiterList<span class="token punctuation">)</span><span class="token punctuation">;</span>
  4. <span class="token punctuation">}</span>

}

  1. String args
  2. List personList
  3. personList
  4. personList
  5. personList
  6. personList
  7. personList
  8. personList
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

运行结果:

高于8000的员工姓名:[Tom, Anni, Owen]

3.3 聚合(max/min/count)

maxmincount这些字眼你一定不陌生,没错,在mysql中我们常用它们进行数据统计。Java stream中也引入了这些概念和用法,极大地方便了我们对集合、数组的数据统计工作。
Java8新特性-Stream - 图6
案例一:获取String集合中最长的元素。
publicclassStreamTest{publicstaticvoidmain([]){=.asList(“adnm”,”admmt”,”pot”,”xbangd”,”weoujgsd”);

  1. Optional<span class="token generics function"><span class="token punctuation">&lt;</span>String<span class="token punctuation">&gt;</span></span> max <span class="token operator">=</span> list<span class="token punctuation">.</span><span class="token function">stream</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">max</span><span class="token punctuation">(</span>Comparator<span class="token punctuation">.</span><span class="token function">comparing</span><span class="token punctuation">(</span>String<span class="token operator">:</span><span class="token operator">:</span>length<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  2. System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"最长的字符串:"</span> <span class="token operator">+</span> max<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  3. <span class="token punctuation">}</span>

}

  1. String args
  2. List list Arrays
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

输出结果:

最长的字符串:weoujgsd

案例二:获取Integer集合中的最大值。
publicclassStreamTest{publicstaticvoidmain([]){=.asList(7,6,9,4,11,6);

  1. <span class="token comment">// 自然排序</span>
  2. Optional<span class="token generics function"><span class="token punctuation">&lt;</span>Integer<span class="token punctuation">&gt;</span></span> max <span class="token operator">=</span> list<span class="token punctuation">.</span><span class="token function">stream</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">max</span><span class="token punctuation">(</span>Integer<span class="token operator">:</span><span class="token operator">:</span>compareTo<span class="token punctuation">)</span><span class="token punctuation">;</span>
  3. <span class="token comment">// 自定义排序</span>
  4. Optional<span class="token generics function"><span class="token punctuation">&lt;</span>Integer<span class="token punctuation">&gt;</span></span> max2 <span class="token operator">=</span> list<span class="token punctuation">.</span><span class="token function">stream</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">max</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">Comparator</span><span class="token generics function"><span class="token punctuation">&lt;</span>Integer<span class="token punctuation">&gt;</span></span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span>
  5. <span class="token annotation punctuation">@Override</span>
  6. <span class="token keyword">public</span> <span class="token keyword">int</span> <span class="token function">compare</span><span class="token punctuation">(</span>Integer o1<span class="token punctuation">,</span> Integer o2<span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span>
  7. <span class="token keyword">return</span> o1<span class="token punctuation">.</span><span class="token function">compareTo</span><span class="token punctuation">(</span>o2<span class="token punctuation">)</span><span class="token punctuation">;</span>
  8. <span class="token punctuation">}</span>
  9. <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  10. System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"自然排序的最大值:"</span> <span class="token operator">+</span> max<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  11. System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"自定义排序的最大值:"</span> <span class="token operator">+</span> max2<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  12. <span class="token punctuation">}</span>

}

  1. String args
  2. List list Arrays
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

输出结果:

自然排序的最大值:11 自定义排序的最大值:11

案例三:获取员工工资最高的人。
publicclassStreamTest{publicstaticvoidmain([]){=newArrayList();.add(newPerson(“Tom”,8900,23,”male”,”New York”));.add(newPerson(“Jack”,7000,25,”male”,”Washington”));.add(newPerson(“Lily”,7800,21,”female”,”Washington”));.add(newPerson(“Anni”,8200,24,”female”,”New York”));.add(newPerson(“Owen”,9500,25,”male”,”New York”));.add(newPerson(“Alisa”,7900,26,”female”,”New York”));

  1. Optional<span class="token generics function"><span class="token punctuation">&lt;</span>Person<span class="token punctuation">&gt;</span></span> max <span class="token operator">=</span> personList<span class="token punctuation">.</span><span class="token function">stream</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">max</span><span class="token punctuation">(</span>Comparator<span class="token punctuation">.</span><span class="token function">comparingInt</span><span class="token punctuation">(</span>Person<span class="token operator">:</span><span class="token operator">:</span>getSalary<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  2. System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"员工工资最大值:"</span> <span class="token operator">+</span> max<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getSalary</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  3. <span class="token punctuation">}</span>

}

  1. String args
  2. List personList
  3. personList
  4. personList
  5. personList
  6. personList
  7. personList
  8. personList
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

输出结果:

员工工资最大值:9500

案例四:计算Integer集合中大于6的元素的个数。
import..;import..;
public class StreamTest {
public static void main(String[] args) {
List list = Arrays.asList(7, 6, 4, 8, 2, 11, 9);

  1. <span class="token keyword">long</span> count <span class="token operator">=</span> list<span class="token punctuation">.</span><span class="token function">stream</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">filter</span><span class="token punctuation">(</span>x <span class="token operator">-</span><span class="token operator">&gt;</span> x <span class="token operator">&gt;</span> <span class="token number">6</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">count</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  2. System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"list中大于6的元素个数:"</span> <span class="token operator">+</span> count<span class="token punctuation">)</span><span class="token punctuation">;</span>
  3. <span class="token punctuation">}</span>

}

  1. javautilArrays
  2. javautilList
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

输出结果:

list中大于6的元素个数:4

3.4 映射(map/flatMap)

映射,可以将一个流的元素按照一定的映射规则映射到另一个流中。分为mapflatMap

  • map:接收一个函数作为参数,该函数会被应用到每个元素上,并将其映射成一个新的元素。
  • flatMap:接收一个函数作为参数,将流中的每个值都换成另一个流,然后把所有流连接成一个流。

Java8新特性-Stream - 图7
Java8新特性-Stream - 图8
案例一:英文字符串数组的元素全部改为大写。整数数组每个元素+3。
publicclassStreamTest{publicstaticvoidmain([]){[]={“abcd”,”bcdd”,”defde”,”fTr”};=.stream().map(::).collect(.toList());

  1. List<span class="token generics function"><span class="token punctuation">&lt;</span>Integer<span class="token punctuation">&gt;</span></span> intList <span class="token operator">=</span> Arrays<span class="token punctuation">.</span><span class="token function">asList</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">9</span><span class="token punctuation">,</span> <span class="token number">11</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  2. List<span class="token generics function"><span class="token punctuation">&lt;</span>Integer<span class="token punctuation">&gt;</span></span> intListNew <span class="token operator">=</span> intList<span class="token punctuation">.</span><span class="token function">stream</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">map</span><span class="token punctuation">(</span>x <span class="token operator">-</span><span class="token operator">&gt;</span> x <span class="token operator">+</span> <span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">collect</span><span class="token punctuation">(</span>Collectors<span class="token punctuation">.</span><span class="token function">toList</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  3. System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"每个元素大写:"</span> <span class="token operator">+</span> strList<span class="token punctuation">)</span><span class="token punctuation">;</span>
  4. System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"每个元素+3:"</span> <span class="token operator">+</span> intListNew<span class="token punctuation">)</span><span class="token punctuation">;</span>
  5. <span class="token punctuation">}</span>

}

  1. String args
  2. String strArr
  3. List strList ArraysstrArrStringtoUpperCaseCollectors
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

输出结果:

每个元素大写:[ABCD, BCDD, DEFDE, FTR] 每个元素+3:[4, 6, 8, 10, 12, 14]

案例二:将员工的薪资全部增加1000。
publicclassStreamTest{publicstaticvoidmain([]){=newArrayList();.add(newPerson(“Tom”,8900,23,”male”,”New York”));.add(newPerson(“Jack”,7000,25,”male”,”Washington”));.add(newPerson(“Lily”,7800,21,”female”,”Washington”));.add(newPerson(“Anni”,8200,24,”female”,”New York”));.add(newPerson(“Owen”,9500,25,”male”,”New York”));.add(newPerson(“Alisa”,7900,26,”female”,”New York”));

  1. <span class="token comment">// 不改变原来员工集合的方式</span>
  2. List<span class="token generics function"><span class="token punctuation">&lt;</span>Person<span class="token punctuation">&gt;</span></span> personListNew <span class="token operator">=</span> personList<span class="token punctuation">.</span><span class="token function">stream</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">map</span><span class="token punctuation">(</span>person <span class="token operator">-</span><span class="token operator">&gt;</span> <span class="token punctuation">{<!-- --></span>
  3. Person personNew <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Person</span><span class="token punctuation">(</span>person<span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> null<span class="token punctuation">,</span> null<span class="token punctuation">)</span><span class="token punctuation">;</span>
  4. personNew<span class="token punctuation">.</span><span class="token function">setSalary</span><span class="token punctuation">(</span>person<span class="token punctuation">.</span><span class="token function">getSalary</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token number">10000</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  5. <span class="token keyword">return</span> personNew<span class="token punctuation">;</span>
  6. <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">collect</span><span class="token punctuation">(</span>Collectors<span class="token punctuation">.</span><span class="token function">toList</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  7. System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"一次改动前:"</span> <span class="token operator">+</span> personList<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">"--&gt;"</span> <span class="token operator">+</span> personList<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getSalary</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  8. System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"一次改动后:"</span> <span class="token operator">+</span> personListNew<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">"--&gt;"</span> <span class="token operator">+</span> personListNew<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getSalary</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  9. <span class="token comment">// 改变原来员工集合的方式</span>
  10. List<span class="token generics function"><span class="token punctuation">&lt;</span>Person<span class="token punctuation">&gt;</span></span> personListNew2 <span class="token operator">=</span> personList<span class="token punctuation">.</span><span class="token function">stream</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">map</span><span class="token punctuation">(</span>person <span class="token operator">-</span><span class="token operator">&gt;</span> <span class="token punctuation">{<!-- --></span>
  11. person<span class="token punctuation">.</span><span class="token function">setSalary</span><span class="token punctuation">(</span>person<span class="token punctuation">.</span><span class="token function">getSalary</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token number">10000</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  12. <span class="token keyword">return</span> person<span class="token punctuation">;</span>
  13. <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">collect</span><span class="token punctuation">(</span>Collectors<span class="token punctuation">.</span><span class="token function">toList</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  14. System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"二次改动前:"</span> <span class="token operator">+</span> personList<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">"--&gt;"</span> <span class="token operator">+</span> personListNew<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getSalary</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  15. System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"二次改动后:"</span> <span class="token operator">+</span> personListNew2<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">"--&gt;"</span> <span class="token operator">+</span> personListNew<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getSalary</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  16. <span class="token punctuation">}</span>

}

  1. String args
  2. List personList
  3. personList
  4. personList
  5. personList
  6. personList
  7. personList
  8. personList
  • 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

案例三:将两个字符数组合并成一个新的字符数组。
publicclassStreamTest{publicstaticvoidmain([]){=.asList(“m,k,l,a”,”1,3,5,7”);=.stream().flatMap(->{// 将每个元素转换成一个stream[]=.split(“,”);=.stream();return;}).collect(.toList());

  1. System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"处理前的集合:"</span> <span class="token operator">+</span> list<span class="token punctuation">)</span><span class="token punctuation">;</span>
  2. System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"处理后的集合:"</span> <span class="token operator">+</span> listNew<span class="token punctuation">)</span><span class="token punctuation">;</span>
  3. <span class="token punctuation">}</span>

}

  1. String args
  2. List list Arrays
  3. List listNew lists
  4. String split s
  5. Stream s2 Arrayssplit
  6. s2
  7. Collectors
  • 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)

归约,也称缩减,顾名思义,是把一个流缩减成一个值,能实现对集合求和、求乘积和求最值操作。
Java8新特性-Stream - 图9
案例一:求Integer集合的元素之和、乘积和最大值。
publicclassStreamTest{publicstaticvoidmain([]){=.asList(1,3,2,8,11,4);// 求和方式1=.stream().reduce((,)->+);// 求和方式2=.stream().reduce(::);// 求和方式3=.stream().reduce(0,::);

  1. <span class="token comment">// 求乘积</span>
  2. Optional<span class="token generics function"><span class="token punctuation">&lt;</span>Integer<span class="token punctuation">&gt;</span></span> product <span class="token operator">=</span> list<span class="token punctuation">.</span><span class="token function">stream</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">reduce</span><span class="token punctuation">(</span><span class="token punctuation">(</span>x<span class="token punctuation">,</span> y<span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">&gt;</span> x <span class="token operator">*</span> y<span class="token punctuation">)</span><span class="token punctuation">;</span>
  3. <span class="token comment">// 求最大值方式1</span>
  4. Optional<span class="token generics function"><span class="token punctuation">&lt;</span>Integer<span class="token punctuation">&gt;</span></span> max <span class="token operator">=</span> list<span class="token punctuation">.</span><span class="token function">stream</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">reduce</span><span class="token punctuation">(</span><span class="token punctuation">(</span>x<span class="token punctuation">,</span> y<span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">&gt;</span> x <span class="token operator">&gt;</span> y <span class="token operator">?</span> x <span class="token operator">:</span> y<span class="token punctuation">)</span><span class="token punctuation">;</span>
  5. <span class="token comment">// 求最大值写法2</span>
  6. Integer max2 <span class="token operator">=</span> list<span class="token punctuation">.</span><span class="token function">stream</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">reduce</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> Integer<span class="token operator">:</span><span class="token operator">:</span>max<span class="token punctuation">)</span><span class="token punctuation">;</span>
  7. System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"list求和:"</span> <span class="token operator">+</span> sum<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">","</span> <span class="token operator">+</span> sum2<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">","</span> <span class="token operator">+</span> sum3<span class="token punctuation">)</span><span class="token punctuation">;</span>
  8. System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"list求积:"</span> <span class="token operator">+</span> product<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  9. System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"list求和:"</span> <span class="token operator">+</span> max<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">","</span> <span class="token operator">+</span> max2<span class="token punctuation">)</span><span class="token punctuation">;</span>
  10. <span class="token punctuation">}</span>

}

  1. String args
  2. List list Arrays
  3. Optional sum listx y x y
  4. Optional sum2 listIntegersum
  5. Integer sum3 list Integersum
  • 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

案例二:求所有员工的工资之和和最高工资。
publicclassStreamTest{publicstaticvoidmain([]){=newArrayList();.add(newPerson(“Tom”,8900,23,”male”,”New York”));.add(newPerson(“Jack”,7000,25,”male”,”Washington”));.add(newPerson(“Lily”,7800,21,”female”,”Washington”));.add(newPerson(“Anni”,8200,24,”female”,”New York”));.add(newPerson(“Owen”,9500,25,”male”,”New York”));.add(newPerson(“Alisa”,7900,26,”female”,”New York”));

  1. <span class="token comment">// 求工资之和方式1:</span>
  2. Optional<span class="token generics function"><span class="token punctuation">&lt;</span>Integer<span class="token punctuation">&gt;</span></span> sumSalary <span class="token operator">=</span> personList<span class="token punctuation">.</span><span class="token function">stream</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">map</span><span class="token punctuation">(</span>Person<span class="token operator">:</span><span class="token operator">:</span>getSalary<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">reduce</span><span class="token punctuation">(</span>Integer<span class="token operator">:</span><span class="token operator">:</span>sum<span class="token punctuation">)</span><span class="token punctuation">;</span>
  3. <span class="token comment">// 求工资之和方式2:</span>
  4. Integer sumSalary2 <span class="token operator">=</span> personList<span class="token punctuation">.</span><span class="token function">stream</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">reduce</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token punctuation">(</span>sum<span class="token punctuation">,</span> p<span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">&gt;</span> sum <span class="token operator">+=</span> p<span class="token punctuation">.</span><span class="token function">getSalary</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
  5. <span class="token punctuation">(</span>sum1<span class="token punctuation">,</span> sum2<span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">&gt;</span> sum1 <span class="token operator">+</span> sum2<span class="token punctuation">)</span><span class="token punctuation">;</span>
  6. <span class="token comment">// 求工资之和方式3:</span>
  7. Integer sumSalary3 <span class="token operator">=</span> personList<span class="token punctuation">.</span><span class="token function">stream</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">reduce</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token punctuation">(</span>sum<span class="token punctuation">,</span> p<span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">&gt;</span> sum <span class="token operator">+=</span> p<span class="token punctuation">.</span><span class="token function">getSalary</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> Integer<span class="token operator">:</span><span class="token operator">:</span>sum<span class="token punctuation">)</span><span class="token punctuation">;</span>
  8. <span class="token comment">// 求最高工资方式1:</span>
  9. Integer maxSalary <span class="token operator">=</span> personList<span class="token punctuation">.</span><span class="token function">stream</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">reduce</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token punctuation">(</span>max<span class="token punctuation">,</span> p<span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">&gt;</span> max <span class="token operator">&gt;</span> p<span class="token punctuation">.</span><span class="token function">getSalary</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">?</span> max <span class="token operator">:</span> p<span class="token punctuation">.</span><span class="token function">getSalary</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
  10. Integer<span class="token operator">:</span><span class="token operator">:</span>max<span class="token punctuation">)</span><span class="token punctuation">;</span>
  11. <span class="token comment">// 求最高工资方式2:</span>
  12. Integer maxSalary2 <span class="token operator">=</span> personList<span class="token punctuation">.</span><span class="token function">stream</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">reduce</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token punctuation">(</span>max<span class="token punctuation">,</span> p<span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">&gt;</span> max <span class="token operator">&gt;</span> p<span class="token punctuation">.</span><span class="token function">getSalary</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">?</span> max <span class="token operator">:</span> p<span class="token punctuation">.</span><span class="token function">getSalary</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
  13. <span class="token punctuation">(</span>max1<span class="token punctuation">,</span> max2<span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">&gt;</span> max1 <span class="token operator">&gt;</span> max2 <span class="token operator">?</span> max1 <span class="token operator">:</span> max2<span class="token punctuation">)</span><span class="token punctuation">;</span>
  14. System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"工资之和:"</span> <span class="token operator">+</span> sumSalary<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">","</span> <span class="token operator">+</span> sumSalary2 <span class="token operator">+</span> <span class="token string">","</span> <span class="token operator">+</span> sumSalary3<span class="token punctuation">)</span><span class="token punctuation">;</span>
  15. System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"最高工资:"</span> <span class="token operator">+</span> maxSalary <span class="token operator">+</span> <span class="token string">","</span> <span class="token operator">+</span> maxSalary2<span class="token punctuation">)</span><span class="token punctuation">;</span>
  16. <span class="token punctuation">}</span>

}

  1. String args
  2. List personList
  3. personList
  4. personList
  5. personList
  6. personList
  7. personList
  8. personList
  • 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)

因为流不存储数据,那么在流中的数据完成处理后,需要将流中的数据重新归集到新的集合里。toListtoSettoMap比较常用,另外还有toCollectiontoConcurrentMap等复杂一些的用法。
下面用一个案例演示toListtoSettoMap
publicclassStreamTest{publicstaticvoidmain([]){=.asList(1,6,3,4,6,7,9,6,20);=.stream().filter(->%2==0).collect(.toList());=.stream().filter(->%2==0).collect(.toSet());

  1. List<span class="token generics function"><span class="token punctuation">&lt;</span>Person<span class="token punctuation">&gt;</span></span> personList <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ArrayList</span><span class="token generics function"><span class="token punctuation">&lt;</span>Person<span class="token punctuation">&gt;</span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  2. personList<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">Person</span><span class="token punctuation">(</span><span class="token string">"Tom"</span><span class="token punctuation">,</span> <span class="token number">8900</span><span class="token punctuation">,</span> <span class="token number">23</span><span class="token punctuation">,</span> <span class="token string">"male"</span><span class="token punctuation">,</span> <span class="token string">"New York"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  3. personList<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">Person</span><span class="token punctuation">(</span><span class="token string">"Jack"</span><span class="token punctuation">,</span> <span class="token number">7000</span><span class="token punctuation">,</span> <span class="token number">25</span><span class="token punctuation">,</span> <span class="token string">"male"</span><span class="token punctuation">,</span> <span class="token string">"Washington"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  4. personList<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">Person</span><span class="token punctuation">(</span><span class="token string">"Lily"</span><span class="token punctuation">,</span> <span class="token number">7800</span><span class="token punctuation">,</span> <span class="token number">21</span><span class="token punctuation">,</span> <span class="token string">"female"</span><span class="token punctuation">,</span> <span class="token string">"Washington"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  5. personList<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">Person</span><span class="token punctuation">(</span><span class="token string">"Anni"</span><span class="token punctuation">,</span> <span class="token number">8200</span><span class="token punctuation">,</span> <span class="token number">24</span><span class="token punctuation">,</span> <span class="token string">"female"</span><span class="token punctuation">,</span> <span class="token string">"New York"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  6. Map<span class="token operator">&lt;</span><span class="token operator">?</span><span class="token punctuation">,</span> Person<span class="token operator">&gt;</span> map <span class="token operator">=</span> personList<span class="token punctuation">.</span><span class="token function">stream</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">filter</span><span class="token punctuation">(</span>p <span class="token operator">-</span><span class="token operator">&gt;</span> p<span class="token punctuation">.</span><span class="token function">getSalary</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">&gt;</span> <span class="token number">8000</span><span class="token punctuation">)</span>
  7. <span class="token punctuation">.</span><span class="token function">collect</span><span class="token punctuation">(</span>Collectors<span class="token punctuation">.</span><span class="token function">toMap</span><span class="token punctuation">(</span>Person<span class="token operator">:</span><span class="token operator">:</span>getName<span class="token punctuation">,</span> p <span class="token operator">-</span><span class="token operator">&gt;</span> p<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  8. System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"toList:"</span> <span class="token operator">+</span> listNew<span class="token punctuation">)</span><span class="token punctuation">;</span>
  9. System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"toSet:"</span> <span class="token operator">+</span> set<span class="token punctuation">)</span><span class="token punctuation">;</span>
  10. System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"toMap:"</span> <span class="token operator">+</span> map<span class="token punctuation">)</span><span class="token punctuation">;</span>
  11. <span class="token punctuation">}</span>

}

  1. String args
  2. List list Arrays
  3. List listNew listx x Collectors
  4. Set set listx x Collectors
  • 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
  • 平均值:averagingIntaveragingLongaveragingDouble
  • 最值:maxByminBy
  • 求和:summingIntsummingLongsummingDouble
  • 统计以上所有:summarizingIntsummarizingLongsummarizingDouble

案例:统计员工人数、平均工资、工资总额、最高工资。
publicclassStreamTest{publicstaticvoidmain([]){=newArrayList();.add(newPerson(“Tom”,8900,23,”male”,”New York”));.add(newPerson(“Jack”,7000,25,”male”,”Washington”));.add(newPerson(“Lily”,7800,21,”female”,”Washington”));

  1. <span class="token comment">// 求总数</span>
  2. Long count <span class="token operator">=</span> personList<span class="token punctuation">.</span><span class="token function">stream</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">collect</span><span class="token punctuation">(</span>Collectors<span class="token punctuation">.</span><span class="token function">counting</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  3. <span class="token comment">// 求平均工资</span>
  4. Double average <span class="token operator">=</span> personList<span class="token punctuation">.</span><span class="token function">stream</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">collect</span><span class="token punctuation">(</span>Collectors<span class="token punctuation">.</span><span class="token function">averagingDouble</span><span class="token punctuation">(</span>Person<span class="token operator">:</span><span class="token operator">:</span>getSalary<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  5. <span class="token comment">// 求最高工资</span>
  6. Optional<span class="token generics function"><span class="token punctuation">&lt;</span>Integer<span class="token punctuation">&gt;</span></span> max <span class="token operator">=</span> personList<span class="token punctuation">.</span><span class="token function">stream</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">map</span><span class="token punctuation">(</span>Person<span class="token operator">:</span><span class="token operator">:</span>getSalary<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">collect</span><span class="token punctuation">(</span>Collectors<span class="token punctuation">.</span><span class="token function">maxBy</span><span class="token punctuation">(</span>Integer<span class="token operator">:</span><span class="token operator">:</span>compare<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  7. <span class="token comment">// 求工资之和</span>
  8. Integer sum <span class="token operator">=</span> personList<span class="token punctuation">.</span><span class="token function">stream</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">collect</span><span class="token punctuation">(</span>Collectors<span class="token punctuation">.</span><span class="token function">summingInt</span><span class="token punctuation">(</span>Person<span class="token operator">:</span><span class="token operator">:</span>getSalary<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  9. <span class="token comment">// 一次性统计所有信息</span>
  10. DoubleSummaryStatistics collect <span class="token operator">=</span> personList<span class="token punctuation">.</span><span class="token function">stream</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">collect</span><span class="token punctuation">(</span>Collectors<span class="token punctuation">.</span><span class="token function">summarizingDouble</span><span class="token punctuation">(</span>Person<span class="token operator">:</span><span class="token operator">:</span>getSalary<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  11. System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"员工总数:"</span> <span class="token operator">+</span> count<span class="token punctuation">)</span><span class="token punctuation">;</span>
  12. System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"员工平均工资:"</span> <span class="token operator">+</span> average<span class="token punctuation">)</span><span class="token punctuation">;</span>
  13. System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"员工工资总和:"</span> <span class="token operator">+</span> sum<span class="token punctuation">)</span><span class="token punctuation">;</span>
  14. System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"员工工资所有统计:"</span> <span class="token operator">+</span> collect<span class="token punctuation">)</span><span class="token punctuation">;</span>
  15. <span class="token punctuation">}</span>

}

  1. String args
  2. List personList
  3. personList
  4. personList
  5. personList
  • 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,比如员工按性别分组。有单级分组和多级分组。

Java8新特性-Stream - 图10
案例:将员工按薪资是否高于8000分为两部分;将员工按性别和地区分组
publicclassStreamTest{publicstaticvoidmain([]){=newArrayList();.add(newPerson(“Tom”,8900,”male”,”New York”));.add(newPerson(“Jack”,7000,”male”,”Washington”));.add(newPerson(“Lily”,7800,”female”,”Washington”));.add(newPerson(“Anni”,8200,”female”,”New York”));.add(newPerson(“Owen”,9500,”male”,”New York”));.add(newPerson(“Alisa”,7900,”female”,”New York”));

  1. <span class="token comment">// 将员工按薪资是否高于8000分组</span>
  2. Map<span class="token operator">&lt;</span>Boolean<span class="token punctuation">,</span> List<span class="token generics function"><span class="token punctuation">&lt;</span>Person<span class="token punctuation">&gt;</span></span><span class="token operator">&gt;</span> part <span class="token operator">=</span> personList<span class="token punctuation">.</span><span class="token function">stream</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">collect</span><span class="token punctuation">(</span>Collectors<span class="token punctuation">.</span><span class="token function">partitioningBy</span><span class="token punctuation">(</span>x <span class="token operator">-</span><span class="token operator">&gt;</span> x<span class="token punctuation">.</span><span class="token function">getSalary</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">&gt;</span> <span class="token number">8000</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  3. <span class="token comment">// 将员工按性别分组</span>
  4. Map<span class="token operator">&lt;</span>String<span class="token punctuation">,</span> List<span class="token generics function"><span class="token punctuation">&lt;</span>Person<span class="token punctuation">&gt;</span></span><span class="token operator">&gt;</span> group <span class="token operator">=</span> personList<span class="token punctuation">.</span><span class="token function">stream</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">collect</span><span class="token punctuation">(</span>Collectors<span class="token punctuation">.</span><span class="token function">groupingBy</span><span class="token punctuation">(</span>Person<span class="token operator">:</span><span class="token operator">:</span>getSex<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  5. <span class="token comment">// 将员工先按性别分组,再按地区分组</span>
  6. Map<span class="token operator">&lt;</span>String<span class="token punctuation">,</span> Map<span class="token operator">&lt;</span>String<span class="token punctuation">,</span> List<span class="token generics function"><span class="token punctuation">&lt;</span>Person<span class="token punctuation">&gt;</span></span><span class="token operator">&gt;&gt;</span> group2 <span class="token operator">=</span> personList<span class="token punctuation">.</span><span class="token function">stream</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">collect</span><span class="token punctuation">(</span>Collectors<span class="token punctuation">.</span><span class="token function">groupingBy</span><span class="token punctuation">(</span>Person<span class="token operator">:</span><span class="token operator">:</span>getSex<span class="token punctuation">,</span> Collectors<span class="token punctuation">.</span><span class="token function">groupingBy</span><span class="token punctuation">(</span>Person<span class="token operator">:</span><span class="token operator">:</span>getArea<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  7. System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"员工按薪资是否大于8000分组情况:"</span> <span class="token operator">+</span> part<span class="token punctuation">)</span><span class="token punctuation">;</span>
  8. System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"员工按性别分组情况:"</span> <span class="token operator">+</span> group<span class="token punctuation">)</span><span class="token punctuation">;</span>
  9. System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"员工按性别、地区:"</span> <span class="token operator">+</span> group2<span class="token punctuation">)</span><span class="token punctuation">;</span>
  10. <span class="token punctuation">}</span>

}

  1. String args
  2. List personList
  3. personList
  4. personList
  5. personList
  6. personList
  7. personList
  8. personList
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

输出结果:
8000{false=[.@2d98a335,.@16b98e56,.@7ef20235],true=[.@27d6c5e0,.@4f3f5b24,.@15aeb7ab]}{=[.@16b98e56,.@4f3f5b24,.@7ef20235],=[.@27d6c5e0,.@2d98a335,.@15aeb7ab]}{={York=[.@4f3f5b24,.@7ef20235],=[.@16b98e56]},={York=[.@27d6c5e0,.@15aeb7ab],=[.@2d98a335]}}

  1. 员工按薪资是否大于分组情况:mutestPerson mutestPerson mutestPerson mutestPerson mutestPerson mutestPerson
  2. 员工按性别分组情况:femalemutestPerson mutestPerson mutestPerson malemutestPerson mutestPerson mutestPerson
  3. 员工按性别、地区:femaleNew mutestPerson mutestPerson WashingtonmutestPerson maleNew mutestPerson mutestPerson WashingtonmutestPerson
  • 1
  • 2
  • 3

    3.6.4 接合(joining)

    joining可以将stream中的元素用特定的连接符(没有的话,则直接连接)连接成一个字符串。
    publicclassStreamTest{publicstaticvoidmain([]){=newArrayList();.add(newPerson(“Tom”,8900,23,”male”,”New York”));.add(newPerson(“Jack”,7000,25,”male”,”Washington”));.add(newPerson(“Lily”,7800,21,”female”,”Washington”));

    1. String names <span class="token operator">=</span> personList<span class="token punctuation">.</span><span class="token function">stream</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">map</span><span class="token punctuation">(</span>p <span class="token operator">-</span><span class="token operator">&gt;</span> p<span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">collect</span><span class="token punctuation">(</span>Collectors<span class="token punctuation">.</span><span class="token function">joining</span><span class="token punctuation">(</span><span class="token string">","</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    2. System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"所有员工的姓名:"</span> <span class="token operator">+</span> names<span class="token punctuation">)</span><span class="token punctuation">;</span>
    3. List<span class="token generics function"><span class="token punctuation">&lt;</span>String<span class="token punctuation">&gt;</span></span> list <span class="token operator">=</span> Arrays<span class="token punctuation">.</span><span class="token function">asList</span><span class="token punctuation">(</span><span class="token string">"A"</span><span class="token punctuation">,</span> <span class="token string">"B"</span><span class="token punctuation">,</span> <span class="token string">"C"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    4. String string <span class="token operator">=</span> list<span class="token punctuation">.</span><span class="token function">stream</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">collect</span><span class="token punctuation">(</span>Collectors<span class="token punctuation">.</span><span class="token function">joining</span><span class="token punctuation">(</span><span class="token string">"-"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    5. System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"拼接后的字符串:"</span> <span class="token operator">+</span> string<span class="token punctuation">)</span><span class="token punctuation">;</span>
    6. <span class="token punctuation">}</span>

    }

    1. String args
    2. List personList
    3. personList
    4. personList
    5. personList
  • 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方法,增加了对自定义归约的支持。
publicclassStreamTest{publicstaticvoidmain([]){=newArrayList();.add(newPerson(“Tom”,8900,23,”male”,”New York”));.add(newPerson(“Jack”,7000,25,”male”,”Washington”));.add(newPerson(“Lily”,7800,21,”female”,”Washington”));

  1. <span class="token comment">// 每个员工减去起征点后的薪资之和(这个例子并不严谨,但一时没想到好的例子)</span>
  2. Integer sum <span class="token operator">=</span> personList<span class="token punctuation">.</span><span class="token function">stream</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">collect</span><span class="token punctuation">(</span>Collectors<span class="token punctuation">.</span><span class="token function">reducing</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> Person<span class="token operator">:</span><span class="token operator">:</span>getSalary<span class="token punctuation">,</span> <span class="token punctuation">(</span>i<span class="token punctuation">,</span> j<span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">&gt;</span> <span class="token punctuation">(</span>i <span class="token operator">+</span> j <span class="token operator">-</span> <span class="token number">5000</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  3. System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"员工扣税薪资总和:"</span> <span class="token operator">+</span> sum<span class="token punctuation">)</span><span class="token punctuation">;</span>
  4. <span class="token comment">// stream的reduce</span>
  5. Optional<span class="token generics function"><span class="token punctuation">&lt;</span>Integer<span class="token punctuation">&gt;</span></span> sum2 <span class="token operator">=</span> personList<span class="token punctuation">.</span><span class="token function">stream</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">map</span><span class="token punctuation">(</span>Person<span class="token operator">:</span><span class="token operator">:</span>getSalary<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">reduce</span><span class="token punctuation">(</span>Integer<span class="token operator">:</span><span class="token operator">:</span>sum<span class="token punctuation">)</span><span class="token punctuation">;</span>
  6. System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"员工薪资总和:"</span> <span class="token operator">+</span> sum2<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  7. <span class="token punctuation">}</span>

}

  1. String args
  2. List personList
  3. personList
  4. personList
  5. personList
  • 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排序器自定义排序

案例:将员工按工资由高到低(工资一样则按年龄由大到小)排序
publicclassStreamTest{publicstaticvoidmain([]){=newArrayList();

  1. personList<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">Person</span><span class="token punctuation">(</span><span class="token string">"Sherry"</span><span class="token punctuation">,</span> <span class="token number">9000</span><span class="token punctuation">,</span> <span class="token number">24</span><span class="token punctuation">,</span> <span class="token string">"female"</span><span class="token punctuation">,</span> <span class="token string">"New York"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  2. personList<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">Person</span><span class="token punctuation">(</span><span class="token string">"Tom"</span><span class="token punctuation">,</span> <span class="token number">8900</span><span class="token punctuation">,</span> <span class="token number">22</span><span class="token punctuation">,</span> <span class="token string">"male"</span><span class="token punctuation">,</span> <span class="token string">"Washington"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  3. personList<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">Person</span><span class="token punctuation">(</span><span class="token string">"Jack"</span><span class="token punctuation">,</span> <span class="token number">9000</span><span class="token punctuation">,</span> <span class="token number">25</span><span class="token punctuation">,</span> <span class="token string">"male"</span><span class="token punctuation">,</span> <span class="token string">"Washington"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  4. personList<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">Person</span><span class="token punctuation">(</span><span class="token string">"Lily"</span><span class="token punctuation">,</span> <span class="token number">8800</span><span class="token punctuation">,</span> <span class="token number">26</span><span class="token punctuation">,</span> <span class="token string">"male"</span><span class="token punctuation">,</span> <span class="token string">"New York"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  5. personList<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">Person</span><span class="token punctuation">(</span><span class="token string">"Alisa"</span><span class="token punctuation">,</span> <span class="token number">9000</span><span class="token punctuation">,</span> <span class="token number">26</span><span class="token punctuation">,</span> <span class="token string">"female"</span><span class="token punctuation">,</span> <span class="token string">"New York"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  6. <span class="token comment">// 按工资升序排序(自然排序)</span>
  7. List<span class="token generics function"><span class="token punctuation">&lt;</span>String<span class="token punctuation">&gt;</span></span> newList <span class="token operator">=</span> personList<span class="token punctuation">.</span><span class="token function">stream</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">sorted</span><span class="token punctuation">(</span>Comparator<span class="token punctuation">.</span><span class="token function">comparing</span><span class="token punctuation">(</span>Person<span class="token operator">:</span><span class="token operator">:</span>getSalary<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">map</span><span class="token punctuation">(</span>Person<span class="token operator">:</span><span class="token operator">:</span>getName<span class="token punctuation">)</span>
  8. <span class="token punctuation">.</span><span class="token function">collect</span><span class="token punctuation">(</span>Collectors<span class="token punctuation">.</span><span class="token function">toList</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  9. <span class="token comment">// 按工资倒序排序</span>
  10. List<span class="token generics function"><span class="token punctuation">&lt;</span>String<span class="token punctuation">&gt;</span></span> newList2 <span class="token operator">=</span> personList<span class="token punctuation">.</span><span class="token function">stream</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">sorted</span><span class="token punctuation">(</span>Comparator<span class="token punctuation">.</span><span class="token function">comparing</span><span class="token punctuation">(</span>Person<span class="token operator">:</span><span class="token operator">:</span>getSalary<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">reversed</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
  11. <span class="token punctuation">.</span><span class="token function">map</span><span class="token punctuation">(</span>Person<span class="token operator">:</span><span class="token operator">:</span>getName<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">collect</span><span class="token punctuation">(</span>Collectors<span class="token punctuation">.</span><span class="token function">toList</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  12. <span class="token comment">// 先按工资再按年龄升序排序</span>
  13. List<span class="token generics function"><span class="token punctuation">&lt;</span>String<span class="token punctuation">&gt;</span></span> newList3 <span class="token operator">=</span> personList<span class="token punctuation">.</span><span class="token function">stream</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
  14. <span class="token punctuation">.</span><span class="token function">sorted</span><span class="token punctuation">(</span>Comparator<span class="token punctuation">.</span><span class="token function">comparing</span><span class="token punctuation">(</span>Person<span class="token operator">:</span><span class="token operator">:</span>getSalary<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">thenComparing</span><span class="token punctuation">(</span>Person<span class="token operator">:</span><span class="token operator">:</span>getAge<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">map</span><span class="token punctuation">(</span>Person<span class="token operator">:</span><span class="token operator">:</span>getName<span class="token punctuation">)</span>
  15. <span class="token punctuation">.</span><span class="token function">collect</span><span class="token punctuation">(</span>Collectors<span class="token punctuation">.</span><span class="token function">toList</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  16. <span class="token comment">// 先按工资再按年龄自定义排序(降序)</span>
  17. List<span class="token generics function"><span class="token punctuation">&lt;</span>String<span class="token punctuation">&gt;</span></span> newList4 <span class="token operator">=</span> personList<span class="token punctuation">.</span><span class="token function">stream</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">sorted</span><span class="token punctuation">(</span><span class="token punctuation">(</span>p1<span class="token punctuation">,</span> p2<span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">&gt;</span> <span class="token punctuation">{<!-- --></span>
  18. <span class="token keyword">if</span> <span class="token punctuation">(</span>p1<span class="token punctuation">.</span><span class="token function">getSalary</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">==</span> p2<span class="token punctuation">.</span><span class="token function">getSalary</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span>
  19. <span class="token keyword">return</span> p2<span class="token punctuation">.</span><span class="token function">getAge</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-</span> p1<span class="token punctuation">.</span><span class="token function">getAge</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  20. <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{<!-- --></span>
  21. <span class="token keyword">return</span> p2<span class="token punctuation">.</span><span class="token function">getSalary</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-</span> p1<span class="token punctuation">.</span><span class="token function">getSalary</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  22. <span class="token punctuation">}</span>
  23. <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">map</span><span class="token punctuation">(</span>Person<span class="token operator">:</span><span class="token operator">:</span>getName<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">collect</span><span class="token punctuation">(</span>Collectors<span class="token punctuation">.</span><span class="token function">toList</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  24. System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"按工资升序排序:"</span> <span class="token operator">+</span> newList<span class="token punctuation">)</span><span class="token punctuation">;</span>
  25. System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"按工资降序排序:"</span> <span class="token operator">+</span> newList2<span class="token punctuation">)</span><span class="token punctuation">;</span>
  26. System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"先按工资再按年龄升序排序:"</span> <span class="token operator">+</span> newList3<span class="token punctuation">)</span><span class="token punctuation">;</span>
  27. System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"先按工资再按年龄自定义降序排序:"</span> <span class="token operator">+</span> newList4<span class="token punctuation">)</span><span class="token punctuation">;</span>
  28. <span class="token punctuation">}</span>

}

  1. String args
  2. List personList
  • 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 提取/组合

流也可以进行合并、去重、限制、跳过等操作。
Java8新特性-Stream - 图11
Java8新特性-Stream - 图12
Java8新特性-Stream - 图13
publicclassStreamTest{publicstaticvoidmain([]){[]={“a”,”b”,”c”,”d”};[]={“d”,”e”,”f”,”g”};

  1. Stream<span class="token generics function"><span class="token punctuation">&lt;</span>String<span class="token punctuation">&gt;</span></span> stream1 <span class="token operator">=</span> Stream<span class="token punctuation">.</span><span class="token function">of</span><span class="token punctuation">(</span>arr1<span class="token punctuation">)</span><span class="token punctuation">;</span>
  2. Stream<span class="token generics function"><span class="token punctuation">&lt;</span>String<span class="token punctuation">&gt;</span></span> stream2 <span class="token operator">=</span> Stream<span class="token punctuation">.</span><span class="token function">of</span><span class="token punctuation">(</span>arr2<span class="token punctuation">)</span><span class="token punctuation">;</span>
  3. <span class="token comment">// concat:合并两个流 distinct:去重</span>
  4. List<span class="token generics function"><span class="token punctuation">&lt;</span>String<span class="token punctuation">&gt;</span></span> newList <span class="token operator">=</span> Stream<span class="token punctuation">.</span><span class="token function">concat</span><span class="token punctuation">(</span>stream1<span class="token punctuation">,</span> stream2<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">distinct</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">collect</span><span class="token punctuation">(</span>Collectors<span class="token punctuation">.</span><span class="token function">toList</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  5. <span class="token comment">// limit:限制从流中获得前n个数据</span>
  6. List<span class="token generics function"><span class="token punctuation">&lt;</span>Integer<span class="token punctuation">&gt;</span></span> collect <span class="token operator">=</span> Stream<span class="token punctuation">.</span><span class="token function">iterate</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> x <span class="token operator">-</span><span class="token operator">&gt;</span> x <span class="token operator">+</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">limit</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">collect</span><span class="token punctuation">(</span>Collectors<span class="token punctuation">.</span><span class="token function">toList</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  7. <span class="token comment">// skip:跳过前n个数据</span>
  8. List<span class="token generics function"><span class="token punctuation">&lt;</span>Integer<span class="token punctuation">&gt;</span></span> collect2 <span class="token operator">=</span> Stream<span class="token punctuation">.</span><span class="token function">iterate</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> x <span class="token operator">-</span><span class="token operator">&gt;</span> x <span class="token operator">+</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">skip</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">limit</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">collect</span><span class="token punctuation">(</span>Collectors<span class="token punctuation">.</span><span class="token function">toList</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  9. System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"流合并:"</span> <span class="token operator">+</span> newList<span class="token punctuation">)</span><span class="token punctuation">;</span>
  10. System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"limit:"</span> <span class="token operator">+</span> collect<span class="token punctuation">)</span><span class="token punctuation">;</span>
  11. System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"skip:"</span> <span class="token operator">+</span> collect2<span class="token punctuation">)</span><span class="token punctuation">;</span>
  12. <span class="token punctuation">}</span>

}

  1. String args
  2. String arr1
  3. String arr2
  • 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]

4 Stream源码解读

这部分等有时间慢慢分解吧。
好,以上就是全部内容,能坚持看到这里,你一定很有收获,那么动一动拿offer的小手,点个赞再走吧。