java8 新特性 按某个字段排序去重复 以及取出前几条数据等

-- 根据bavatar去重复
rightsCouponList.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(RightsCoupon::getBavatar))), ArrayList::new));

-- 按照RightsCoupon里面的bavatar倒叙排列
rightsCouponList.stream().sorted(Comparator.comparing(RightsCoupon::getBavatar).reversed()).collect(Collectors.toList());

-- 取前三条数据
-- 正序排列:由小到大
-- 按照RightsCoupon里面的bavatar倒叙排列并且取出前三条数据
rightsCouponList.stream().sorted(Comparator.comparing(RightsCoupon::getBavatar).reversed()).limit(3).collect(Collectors.toList());

--按照距离排列和空/Null数据排序  并且取出前20条数据 nullsLast:null放后面 shopDTOList.stream().sorted(Comparator.comparing(CompanyShopDTO::getDistanceNum, Comparator.nullsLast(Long::compareTo))).limit(20).collect(Collectors.toList());

-- 需要正序排列去掉.reversed()  
rightsCouponList.stream().sorted(Comparator.comparing(RightsCoupon::getBavatar)).limit(3).collect(Collectors.toList());

-- 获取某个属性List(bavatar)
rightsCouponList.stream().map(it -> it.getBavatar()).collect(Collectors.toList());

-- 排序 status顺序 createTime 顺序 

Collections.sort(myRedpackDtoList,Comparator.comparing(MyRedpackDto::getStatus).thenComparing(MyRedpack::getCreateTime));

-- 排序 status降序 createTime 降序
myRedpackDtoList.stream().sorted(Comparator.comparing(MyRedpackDto::getStatus).reversed().thenComparing(MyRedpack::getCreateTime).reversed());

Collections.sort(myRedpackDtoList,Comparator.comparing(MyRedpackDto::getStatus).reversed().thenComparing(MyRedpack::getCreateTime).reversed());

-- 按指定字段移除
myRedpackDtoList.removeIf(m -> m.getStatus().contains("a"));

-- 把为空的数据进行过滤掉

List<TbLaborer> laborerList = tbLaborerList.stream().filter((TbLaborer p) -> StringUtils.isNotEmpty(p.getDeviceCode())).map(p -> p).collect(Collectors.toList());

List<String> deviceCodes = tbLaborerList.stream().filter((TbLaborer p) -> StringUtils.isNotEmpty(p.getDeviceCode())).map(p -> p.getDeviceCode()).collect(Collectors.toList());

  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值