此篇是从简书移至本博客, 简书地址:https://www.jianshu.com/u/e79d7856d1ff
一、引入lambda
定义:lambda就是简化了匿名内部类
使用前提:安装JDK 8
通过一个Example一步一步来达到lambda表达式的相关 :
筛选出集合内大于50的数
普通Java代码实现
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19public static void main(String[] args) {
List<Integer> list = new ArrayList<>();
for (int i = 0; i < 10; i++) {
Random random = new Random();
int nextInt = random.nextInt(100) + 1;
list.add(nextInt);
}
System.out.println("原:" + list);
System.out.println("筛选出大于50的数");
filter(list);
}
public static void filter(List<Integer> list) {
for (Integer integer : list) {
if (integer > 50) {
System.out.println(integer);
}
}
}匿名类部类实现
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
29public static void main(String[] args) {
List<Integer> list = new ArrayList<>();
for (int i = 0; i < 10; i++) {
Random random = new Random();
int nextInt = random.nextInt(100) + 1;
list.add(nextInt);
}
System.out.println("原:" + list);
Checker checker = new Checker() {
public boolean check(Integer i) {
return i > 50;
}
};
filter(list, checker);
}
public static void filter(List<Integer> list, Checker checker) {
for (Integer integer : list) {
if (checker.check(integer)) {
System.out.println(integer);
}
}
}
// Checker接口
public interface Checker {
public boolean check(Integer i);
}一步一步演变成lambda表达式
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
36
37
38
39
40
41
42public static void main(String[] args) {
List<Integer> list = new ArrayList<>();
for (int i = 0; i < 10; i++) {
Random random = new Random();
int nextInt = random.nextInt(100) + 1;
list.add(nextInt);
}
Checker c1 = new Checker() {
public boolean check(Integer i) {
return i > 50;
}
};
System.out.println("筛选出大于50的数--->1");
filter(list, c1);
Checker c2 = (Integer i) -> { return i > 50; };
System.out.println("筛选出大于50的数--->2");
filter(list, c2);
Checker c3 = (Integer i) -> i > 50;
System.out.println("筛选出大于50的数--->3");
filter(list, c3);
Checker c4 = i -> i > 50;
System.out.println("筛选出大于50的数--->4");
filter(list, c4);
System.out.println("筛选出大于50的数--->5");
filter(list, i -> i > 50);
List<Integer> is = list.stream().filter(i -> i > 50).collect(Collectors.toList());
System.out.println(is);
}
public static void filter(List<Integer> list, Checker checker) {
for (Integer integer : list) {
if (checker.check(integer)) {
System.out.println(integer);
}
}
}
总结
1 | 1.匿名类的正常写法 |
1 | 2. 把外面的壳子去掉,只保留方法参数和方法体,参数和方法体之间加上符号 -> |
1 | 3. 把return和{}去掉 |
1 | 4. 去掉参数小括号,得到Lambda表达式 |
####排序例子1
2
3
4
5
6
7
8
9
10//TODO 匿名类部类
Collections.sort(list, new Comparator<String>() {
public int compare(String o1, String o2) {
return o1.compareTo(o2);
}
});
// TODO lambda表达式
Collections.sort(list, (o1, o2) -> o1.compareTo(o2));
二、聚合操作
遍历: 传统方式与聚合操作遍历数据
1 | List<Integer> list = new ArrayList<>(); |
Stream和管道的概念
- Stream: Stream是一系列元素, 象生产线上的罐头, 一罐一罐的生产出来
- 管道: 是指一系列聚合操作, 分为:管道源、中间操作、结束操作。
管道源:在前面的例子里面,管道源是list集合
- 将Collection切换到管道源只需要调用stream()方法即可:
1 | list.stream(); |
- 数组却没有stream()方法,需要使用:
1 | int[] arr = {1, 2, 3, 4}; |
中间操作:也叫‘懒’操作,并不会进行变量,每一步中间操作都会返回一个Stream对象
对元素进行筛选
- filter 匹配
- distinct 去除重复(根据equals判断)
- sorted 自然排序
- sorted(Comparator
) 指定排序 - limit 保留
- skip 忽略
转换为其他形式的流
- mapToDouble 转换为double的流
- map 转换为任意类型的流
1 | //TODO 去重排序打印 |
结束操作: 结束操作不会返回Stream,但是会返回int、float、String、 Collection或者像forEach或者什么都不返回
- forEach() 遍历每个元素
- toArray() 转换为数组
- min(Comparator
) 取最小的元素 - max(Comparator
) 取最大的元素 - count() 总数
- findFirst() 第一个元素
1 | // 遍历集合中的每个数据 |