1、对BigDecimal类型的结果集进行求和
list.stream().map(AchievementCount::getTotalCashTarget).reduce(BigDecimal.ZERO,BigDecimal::add);
2、对Long类型的结果集进行求和
list.stream().mapToLong(Cost::getCost).sum()
3、对list中的指定字段进行过滤
list.stream().filter(t -> t.getDeptName().contains(deptName)).collect(Collectors.toList());
4、对list中的指定字段进分组
Map<String, List<Account>> map =accounts.stream().collect(Collectors.groupingBy(Account::getContract_party));
5、对list中的指定字段进排序(倒序)
方法一:Comparator<AccountTaskListDto> c1 = Comparator.comparing(root -> root.getExpend());
Collections.sort(list, c1.reversed());
方法二:dataList.sort(comparing(TotalData::getCost).reversed());
6、对list进行循环
list.forEach(t -> { if (null == t.getStatus()) { ownerIs.add(t.getOwnerId()); } });//在需要其他条件成立下使用
list.forEach(t -> updateAccount(t));//可以直接这样循环调用修改方法
7、两个list进行去重操作,保留集合一的不重复数据
List<AccountVo> list = response.getData().getItems();//集合一
List<Account> towList = accountRepository.findAll(); //集合二
List<AccountVo> distinctByUniqueList = list.stream().filter(item -> !towList.stream().map(e -> e.getOwnerId()).collect(Collectors.toList()).contains(item.getOwnerId())).collect(Collectors.toList());
8、list根据条件进行删除数据
list.removeIf(t -> t.getDuties().contains("运营"));
9、list有序分组,主要是在分组中指明分组类型
Map<String, List<ProxyTempData>> dayMap = threeDataList.stream().collect(Collectors.groupingBy(ProxyTempData::getDate,LinkedHashMap::new,Collectors.toList()));
10、两个list集合根据id进行比较,匹配则进行赋值操作,否则返回null
//得到该list中所有筏工对应的排筏id
List<RaftSettlementStatisticsResultDto> raftIdList = iRaftSettlementStatisticsMapper.getRaftIdList(operatorIds);
//把排筏id给到对应的对象中
list.stream().map(t -> raftIdList.stream().filter(r -> r.getOperatorId().equals(t.getOperatorId())).findFirst().map(s -> {
t.setBoardingIds(s.getRaftIds());
return t;
}).orElse(null)).collect(Collectors.toList());
11、从list循环中取出指定的单个对象,get().getLineName()是取出这单个对象中的某一个属性值
cruiseLineList.stream().filter(s -> s.getCruiseLineId() == t.getCruiseLineId()).findAny().get().getLineName()
12、取出list对象中不为null的某个属性集合
List<String> userIds = list.stream().map(t -> t.getSettledSystemUserId()).filter(t -> Objects.nonNull(t)).collect(Collectors.toList());
13、从list中取匹配的值判空,userList不一定存在对应的id,否则会提示空指针异常
list.forEach(t -> {
Optional<SystemUser> optional = userList.stream().filter(s -> s.getSystemUserId().equals(t.getSettledSystemUserId())).findAny();
if(optional != null && optional.isPresent()){
t.setSettledSystemUserName(optional.get().getSystemUserName());
}
});
14、对list对象分组并求和
Map<String, Long> map = list.stream().collect(Collectors.groupingBy(PurchaseOrderExcelDto::getConsumeSerial, Collectors.counting()));
15、对map集合进行判断并取出满足条件的key
Boolean flag = map.entrySet().stream().anyMatch(e -> e.getValue() > 1);
if (flag) {
List<String> keyList = map.entrySet().stream().filter(key -> key.getValue() > 1).map(t -> t.getKey()).collect(Collectors.toList());
}
16、list集合转map并自定义key
List<SystemDictionary> dictionaryList = systemDictionaryDao.allSystemDictionary();
if (ObjectUtil.isNotEmpty(dictionaryList)) {
Map<String, SystemDictionary> partsMap = dictionaryList.stream().collect(Collectors.toMap(t -> t.getPcode() + "_" + t.getCode(), t -> t));
dictionaryMap.putAll(partsMap);
}