jdk8常用lambda表达式

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());

扫描二维码关注公众号,回复: 5686985 查看本文章

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("运营"));

 
 

猜你喜欢

转载自blog.csdn.net/baidu_37302589/article/details/88850115