Java Stream API 常用方法笔记
·
Java Stream API 常用方法笔记
1. stream()
作用
将集合转换成 Stream 流,开启链式处理。
示例
list.stream()
理解
List集合
|
↓
Stream流
|
↓
进行过滤、转换等操作
注意:
- stream() 不会修改原集合
- stream() 只是开启流处理
2. map()
作用
转换数据,把一个对象转换成另一个对象。
格式
.map(e -> 转换后的数据)
示例
List<Long> ids = list.stream()
.map(e -> e.getId())
.collect(Collectors.toList());
原数据:
User(id=1)
User(id=2)
User(id=3)
转换:
[1,2,3]
常见用途
- 获取对象某个字段
- Entity 转 DTO
- 对象类型转换
例如:
.map(UserDTO::new)
表示:
User对象
↓
UserDTO对象
3. filter()
作用
过滤数据,只保留满足条件的数据。
格式
.filter(条件)
示例
List<User> users = list.stream()
.filter(e -> e.getAge() > 18)
.collect(Collectors.toList());
原数据:
10岁
20岁
30岁
过滤后:
20岁
30岁
常见写法
过滤 null:
.filter(Objects::nonNull)
作用:
删除 null 数据。
例如:
原:
[
null,
User,
null,
User
]
结果:
[
User,
User
]
4. collect()
作用
将 Stream 结果转换成集合。
转 List
.collect(Collectors.toList())
示例:
List<Long> ids =
list.stream()
.map(e -> e.getId())
.collect(Collectors.toList());
结果:
Stream
↓
List
转 Set
.collect(Collectors.toSet())
作用:
去重。
转 Map
.collect(Collectors.toMap())
作用:
转换成 Map。
5. forEach()
作用
遍历执行操作。
示例:
list.stream()
.forEach(e -> System.out.println(e));
等价:
for(Object e:list){
System.out.println(e);
}
6. sorted()
作用
排序。
默认排序:
list.stream()
.sorted();
自定义排序:
list.stream()
.sorted((a,b)->a.getAge()-b.getAge());
例如:
排序前:
30
10
20
排序后:
10
20
30
7. distinct()
作用
去重。
示例:
list.stream()
.distinct();
原:
1
2
2
3
结果:
1
2
3
8. limit()
作用
获取前 N 条数据。
示例:
list.stream()
.limit(10);
含义:
获取前10条。
9. skip()
作用
跳过前 N 条数据。
示例:
list.stream()
.skip(10);
含义:
跳过前10条。
10. findFirst()
作用
获取第一个符合条件的数据。
示例:
Optional<User> user =
list.stream()
.findFirst();
返回:
Optional<T>
原因:
可能没有数据。
11. anyMatch()
作用
判断是否存在一个满足条件的数据。
示例:
boolean result =
list.stream()
.anyMatch(e -> e.getStatus()==1);
返回:
true / false
12. allMatch()
作用
判断是否全部满足条件。
示例:
boolean result =
list.stream()
.allMatch(e -> e.getStatus()==1);
含义:
所有数据 status 都等于1。
13. noneMatch()
作用
判断是否没有满足条件的数据。
示例:
boolean result =
list.stream()
.noneMatch(e -> e.getStatus()==1);
含义:
没有任何数据 status=1。
14. count()
作用
统计数量。
示例:
long count =
list.stream()
.count();
结果:
集合元素数量
15. reduce()
作用
聚合计算。
例如求和:
int sum =
list.stream()
.reduce(0,(a,b)->a+b);
计算:
1+2+3
结果:
6
常用组合
1. 获取对象ID列表
代码:
List<Long> ids =
list.stream()
.map(e -> e.getId())
.collect(Collectors.toList());
流程:
对象集合
|
↓
获取id
|
↓
List<Long>
例如:
输入:
[
User(id=1),
User(id=2),
User(id=3)
]
输出:
[1,2,3]
2. 过滤数据
代码:
List<User> users =
list.stream()
.filter(e -> e.getStatus()==1)
.collect(Collectors.toList());
流程:
集合
↓
过滤
↓
返回新集合
3. 过滤 + 转换
代码:
List<Long> ids =
list.stream()
.filter(Objects::nonNull)
.map(e -> e.getId())
.collect(Collectors.toList());
流程:
集合
↓
过滤null
↓
获取id
↓
生成List
项目中常见写法
获取数据库ID
List<Long> ids =
collect.stream()
.map(e -> e.getId())
.collect(Collectors.toList());
含义:
历史数据集合
|
↓
获取每条数据id
|
↓
组成id集合
例如:
输入:
[
History(id=100),
History(id=200),
History(id=300)
]
输出:
[100,200,300]
Stream 方法分类
中间操作(返回 Stream)
| 方法 | 作用 |
|---|---|
| map | 转换 |
| filter | 过滤 |
| sorted | 排序 |
| distinct | 去重 |
| limit | 限制数量 |
| skip | 跳过数据 |
终止操作(产生结果)
| 方法 | 作用 |
|---|---|
| collect | 转换集合 |
| forEach | 遍历 |
| findFirst | 查找第一个 |
| count | 统计 |
| reduce | 计算 |
| anyMatch | 是否存在 |
| allMatch | 是否全部 |
| noneMatch | 是否没有 |
记忆口诀
stream 开始处理
filter 筛选数据
map 转换数据
sorted 排序
collect 收集结果
forEach 遍历执行
项目中最常见:
list.stream()
.filter()
.map()
.collect();
理解:
集合
↓
过滤
↓
转换
↓
重新组成集合
更多推荐




所有评论(0)