1. stream 的创建
2. stream 的debug
==> debug运行 走到 stream流 的这一行, 点击运行栏红色框框的地方,就可以看到 stream流 的 整个操作.可以清晰的看到 每一个过程,数据是怎么处理的.对理解stream流,有很大的帮助…
3. stream 的基础操作
4. stream 的收集操作
5. stream 的 常用操作
2.1 转map ,对象本身作为value
@Test
@DisplayName("page")
public void page() {
IPage<User> page1 = userService.page(
new Page<>(1,3),
new LambdaQueryWrapper<User>()
.gt(User::getId,1l));
page1.getRecords().forEach(System.out::println);
List<User> list = page1.getRecords();
//放对象
Map<String, User> collect = list.stream().collect(Collectors.toMap(User::getName, user -> user));
Iterator<Map.Entry<String, User>> iterator = collect.entrySet().iterator();
while (iterator.hasNext()){
Map.Entry<String, User> next = iterator.next();
System.out.println(next.getKey()+"-->"+next.getValue());
}
}
2.2 转map ,对象本身作为value
@Test
@DisplayName("identity")
public void identity() {
List<User> list = userService.list();
Map<Long, User> map = list.stream().collect(Collectors.toMap(User::getId,Function.identity()));
Iterator<Map.Entry<Long, User>> iterator = map.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<Long, User> next = iterator.next();
System.out.println(next.getKey() + "-->" + next.getValue());
}
}
List<ParmRegionVO> regionVOList = pageContent.getContent();
Map<String, ParmRegionVO> parmRegionVOMap = regionVOList.stream().collect(Collectors.toMap(ParmRegionVO::getRegionCode, t -> t, (k, v) -> v));
分组,之后筛选最大的数据
//根据MicroUserId分组,并取UpdatedTime最近的
Map<Long, OauthUser> collect = users.stream().collect(
Collectors.groupingBy(OauthUser::getMicroUserId,
Collectors.collectingAndThen(
Collectors.reducing((a, b) -> a.getUpdatedTime() > b.getUpdatedTime() ? a : b), Optional::get)
)
);