写点什么

Java stream 流用法总结

  • 2022 年 5 月 14 日
  • 本文字数:1204 字

    阅读完需:约 4 分钟

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;

}

用户头像

还未添加个人签名 2022.04.13 加入

还未添加个人简介

评论

发布
暂无评论
Java stream流用法总结_Java_爱好编程进阶_InfoQ写作社区