lambda骚操作

  1. list按属性去重
 bookInfoVos.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparingLong(BookInfoVo::getId))), ArrayList::new));

  1. 根据list中属性进行分组
List<List<ImportErrDTO>> msgList = new ArrayList<>();
errs.stream().collect(Collectors.groupingBy(ImportErrDTO::getMsg, Collectors.toList())).
forEach((importErrMsg, listByMsg) -> msgList.add(listByMsg));

Map<String, List<ProjectValidTaskVo>> groupBySex = task.getTasks().stream().collect(Collectors.groupingBy(ProjectValidTaskVo::getTaskStatus));
  1. 根据list中多个属性进行分组
List<List<Prod>> prodList = new ArrayList<>();
prods.stream().collect(Collectors.groupingBy(i ->(i.getPackageId() + "_" + i.getProductId() + "_" + i.getSaleProductId()),Collectors.toList())).
forEach((prod, listByProd) -> prodList.add(listByProd));
  1. 根据list中属性进行分组并返回MAP
Map<Long, List<Prod>> mapType = prods.stream().collect(Collectors.groupingBy(Prod::getType));
  1. list中属性求和
Long:
prods.stream().map(Prod::getCount).filter(Objects::nonNull).reduce(0L, Long::sum);
Bigdecimal:
prods.stream().map(Prod::getPrice).filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add);
Integer:
prods.stream().map(Prod::getProdCount).filter(Objects::nonNull).reduce(0, Integer::sum);
  1. 根据list中对象转为其他对象再根据其中的属性进行排序
user.stream().map(i -> new User1(i.getId(), i.getName())).sorted(Comparator.comparing(User1::getId)).collect(Collectors.toList());
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值