Java stream 流用法总结
Page<TbMemberDeductionSettingDto> memberDeductionSettingDtoPage = new Page<>();
// 分组统计
Map<String, List<TbMemberDeductionSetting>> collect = records.stream().collect(Collectors.groupingBy(m -> m.getLevelId() + "_" + m.getUserTypeId()));
List<TbMemberDeductionSettingDto> settingDtos = collect.keySet().stream().map(m -> {
TbMemberDeductionSettingDto tbMemberDeductionSettingDto = new TbMemberDeductionSettingDto();
String[] s = m.split("_");
String levelId = s[0];
String userTypeId = s[1];
List<TbMemberDeductionSetting> tbMemberDeductionSettings = collect.get(m);
List<MemberDeductionSettingDto> collect1 = tbMemberDeductionSettings.stream().map(t -> {
MemberDeductionSettingDto memberDeductionSettingDto = new MemberDeductionSettingDto();
memberDeductionSettingDto.setId(t.getId());
memberDeductionSettingDto.setDeductionMoney(t.getDeductionMoney());
memberDeductionSettingDto.setDeductionTypeId(t.getDeductionTypeId());
memberDeductionSettingDto.setDeductionTypeName(t.getDeductionTypeName());
memberDeductionSettingDto.setDeductionValue(t.getDeductionValue());
return memberDeductionSettingDto;
}).collect(Collectors.toList());
tbMemberDeductionSettingDto.setRoleName(tbMemberDeductionSettings.get(0).getRoleName());
tbMemberDeductionSettingDto.setLevelName(tbMemberDeductionSettings.get(0).getLevelName());
tbMemberDeductionSettingDto.setLevelId(Integer.parseInt(levelId));
tbMemberDeductionSettingDto.setUserTypeId(Integer.parseInt(userTypeId));
tbMemberDeductionSettingDto.setList(collect1);
return tbMemberDeductionSettingDto;
}).collect(Collectors.toList());
memberDeductionSettingDtoPage.setRecords(settingDtos);
memberDeductionSettingDtoPage.setTotal(tbMemberDeductionSettingPage.getTotal());
return new R<>(memberDeductionSettingDtoPage);
5.累加 int 型
int orders= list.stream().mapToInt(IncomeInfoDto::getRecommendAnchorAssociateOrders).sum();
6.过滤分割集合
List collect = shopInfoList.stream().map(s -> {
UserAttentionDto userAttentionDto = new UserAttentionDto(); **《一线大厂 Java 面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》无偿开源 威信搜索公众号【编程进阶路】**
userAttentionDto.setUserId(userId);
return userAttentionDto;
}).distinct().skip(pageSize * (page - 1))
.limit(pageSize).collect(Collectors.toList());
if (StringUtil.isNotNull(keyWords)) {
List<UserAttentionDto> collect1 = collect.stream().filter(u -> u.getAttentionName().contains(keyWords)).collect(Collectors.toList());
return collect1;
}
评论