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

  • Post author:
  • Post category:java




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



版权声明:本文为qq_43035664原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。