JDK8 Stream API

参考:
Java Stream API入门篇
Java8 Stream API介绍
Java8之Stream类


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
List<String> strings = Arrays.asList("a", "d", "ga", "dsagafd","adfsafdsa","a","d");
strings.stream().filter(e->e.startsWith("a")).forEach(e-> System.out.println(e));
System.out.println("-------------------");
strings.stream().distinct().forEach(e-> System.out.println(e));
Stream<String> sorted = strings.stream().sorted((a, b) -> b.length() - a.length());
System.out.println("==================");
sorted.forEach(a-> System.out.println(a));
boolean a = strings.stream().anyMatch((s) -> s.equals("a"));
boolean b = strings.stream().allMatch((s) -> s.equals("a"));
boolean c = strings.stream().noneMatch((s) -> s.equals("a"));
System.out.println(a);
System.out.println(b);
System.out.println(c);
---------------------结果--------------------------
a
adfsafdsa
a
-------------------
a
d
ga
dsagafd
adfsafdsa
==================
adfsafdsa
dsagafd
ga
a
d
a
d
true
false
false

对stream的操作分为为两类,中间操作(intermediate operations)和结束操作(terminal operations)
中间操作是惰性化的,每次调用会对Stream做一定的处理,返回一个新的Stream,并没有真正开始计算
每个stream只能有一个结束操作,执行了结束操作之后,stream就被消费掉了。

常用方法:
中间操作:concat() distinct() filter() flatMap() limit() map() peek() skip() sorted() parallel() sequential() unordered()
结束操作:allMatch() anyMatch() collect() count() findAny() findFirst() forEach() forEachOrdered() max() min() noneMatch() reduce() toArray()

sort()有两种方式,对stream进行排序,默认排序和自定义的Comparable形式,例子参见上方
mapflatMap 都是对stream进行遍历操作,都会返回操作之后的stream,不同的是flatMap会将多个集合中的元素全部拿出来放到一个集合中,而map不会
如对两个集合[1,2,3],[8,9,7]
stream之后的map操作的结果是[1,2,3],[8,9,7]
stream之后的flatMap结果是[1,2,3,8,9,7]
filter可以对元素进行过滤,参考上面例子
limit截取stream中的元素
skip跳过stream中的元素
distinct去重,通过equals和hashcode方法,自定义类
anyMatch ,allMatch,noneMatch,stream中的元素有匹配,全部匹配,全都不匹配。
reduce聚合操作

文章作者: C.c
文章链接: https://liquidcat.cc/jdk8-stream-api.html
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 Me