Java8 List新特性

文章展示了JavaStreamAPI的一些核心操作,包括条件筛选、分组、去重、字段提取、求和以及转换为Map。示例涵盖了filter方法用于过滤数据,groupingBy用于按字段分组,collectingAndThen与TreeSet结合实现去重,以及map和reduce方法在求和与构建Map时的应用。
摘要由CSDN通过智能技术生成

条件筛选

AssignCourseType assignCourseType = assignCourseTypeList.stream().filter(f -> f.getOurseName().contains("一次")).findFirst().orElse(null);

分组

Map<Long, List<SalaryStandardVo>> standardUserGroup = salaryStandardList.stream().collect(Collectors.groupingBy(SalaryStandardVo::getUpmsUserId));

去重

List<SalaryStandardVo> distinctSalaryStandardList = salaryStandardList.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(SalaryStandardVo::getIncentiveCode))), ArrayList::new));

获取list对象中的某个字段

List<Long> empIds = deptMangerRes.stream().map(DeptMangerRes::getEmployeeId).collect(Collectors.toList());

求和

int mathSumInt = list.stream().mapToInt( Student::getMathScoresInt ).sum(); //int类型
 long mathSumLong = list.stream().mapToLong( Student::getMathScoresLong ).sum(); //long类型
 double mathSumDouble = list.stream().mapToDouble( Student::getMathScoresDouble ).sum(); //double类型
 BigDecimal mathSumBigDecimal = list.stream().map( Student::getMathScoresBigDecimal ).reduce(BigDecimal.ZERO, BigDecimal::add); //BigDecimal类型

转Map

Map<String,Integer> sourceMap = source.stream().collect(Collectors.toMap(AppTodoType::getSourceName,AppTodoType::getSource));
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值