1)Stream将List转换为Map,使用Collectors.toMap方法进行转换
背景:User类,类中分别有id,name,age三个属性。List集合,userList,存储User对象
1、指定key-value,value是对象中的某个属性值。
Map<Integer,String> userMap1 = userList.stream().collect(Collectors.toMap(User::getId,User::getName));
Map<Integer, Integer> loginNameAndCaseTotalMap = topicPacificCaseLifecycleDailyAggrVOS.stream().collect(Collectors.toMap(bean -> Integer.valueOf(bean.getCloserLoginName()), bean->bean.getCaseTotal()));
2、指定key-value,value是对象本身,User->User 是一个返回本身的lambda表达式
Map<Integer,User> userMap2 = userList.stream().collect(Collectors.toMap(User::getId,User->User));
3、指定key-value,value是对象本身,Function.identity()是简洁写法,也是返回对象本身
Map<Integer,User> userMap3 = userList.stream().collect(Collectors.toMap(User::getId, Function.identity()));
4、指定key-value,value是对象本身,Function.identity()是简洁写法,也是返回对象本身,key 冲突的解决办法,这里选择第二个key覆盖第一个key。
Map<Integer,User> userMap4 = userList.stream().collect(Collectors.toMap(User::getId, Function.identity(),(key1,key2)->key2));
2)计算
1.list某个属性的平均值
Double averageDailyDownloads = hasPassRuleData.stream().mapToInt(FactCsSecretFileDownload::getDownloadCnt).average().orElse(0D);
2.list某个属性的总数
Integer delayClosedCaseCntTotal = appCsWoCaseDelayClosedSummaryDailies.stream().mapToInt(AppCsWoCaseDelayClosedSummaryDaily::getDelayClosedCaseCnt).sum();
BigDecimal refundAmountCount = appCsWoRiskRefundList.stream().map(AppCsWoRiskRefund::getRefundAmount).reduce(BigDecimal.ZERO,BigDecimal::add);
3.list某个属性去重统计个数
long missNum = hasPassRuleData.stream().map(AppCsEmpRoleOverdueRetain::getApplyUserMis).distinct().count();
3)获取list中某个属性最高的bean记录
ChatRiskPassRecord maxRiskLevelRecord = chatRiskPassRecordList.stream().max(new Comparator<ChatRiskPassRecord>() {
@Override
public int compare(ChatRiskPassRecord o1, ChatRiskPassRecord o2) {
return (int) (o2.getRiskLevelId() - o1.getRiskLevelId());
}
}).orElseGet(null);
4)映射
List<Long> riskIds = aggregationRiskRecords.stream().map(riskRecord -> riskRecord.getId()).collect(Collectors.toList());
5)过滤
List<AppCsEmpInterTransferRoleException> riskData = targetData.stream().filter(appCsEmpInterTransferRoleException-> appCsEmpInterTransferRoleException.getRiskAccountType() > 0).collect(Collectors.toList());
interArguments.stream().filter(interArgument -> interArgument.getMapVariable() != null)
.forEach(interArgument -> judgeCallRiskPassParamIn(param, interArgument, jsonNode));
Optional<KeyWordGroup> first = keyWordGroups.stream().filter(group ->
group.getId().longValue() != keyWordGroupId
&& group.getGroupName().equals(keyWordGroup.getGroupName())).findFirst();
6)分组
Map<String, List<AppCsWoCaseDelayClosedSummaryDaily>> closerLoginNameMap = targetData.stream().collect(Collectors.groupingBy(AppCsWoCaseDelayClosedSummaryDaily::getCloserLoginName));
7)排序
fullFavoritesList.addAll(workbenchPatternCountVoList.stream().sorted(Comparator.comparing(WorkbenchPatternCountVo::getPercent).reversed()).collect(Collectors.toList()));
8) list<bean>转换
List<ExtendUiMarkFieldVo> extendUiMarkFieldVos = uiMarkFields.stream().map(uiMarkField ->{ ExtendUiMarkFieldVo extendUiMarkFieldVo = new ExtendUiMarkFieldVo(); extendUiMarkFieldVo.setMarkFieldDefId(uiMarkField.getMarkFieldDefId()); extendUiMarkFieldVo.setFieldIndex(uiMarkField.getFieldIndex()); extendUiMarkFieldVo.setMustFill(uiMarkField.getMustFill()); extendUiMarkFieldVo.setIsDefault(uiMarkField.getIsDefault()); return extendUiMarkFieldVo; }).collect(Collectors.toList());