zoukankan      html  css  js  c++  java
  • java8 实现List对象去重

    举例说明:以用户优惠券为例,全部优惠券去重,可用优惠券去重,以及全部优惠券去除可用优惠券

    
    

    import java.util.stream.Collectors;
    import static java.util.Comparator.comparingLong;
    import static java.util.stream.Collectors.collectingAndThen;
    import static java.util.stream.Collectors.toCollection;


    // 全部优惠券去重
    List<CouponVo> allCouponUniqueList = userAllCouponList.stream().collect(collectingAndThen(toCollection(() -> new TreeSet<>(comparingLong(CouponVo::getId))), ArrayList::new));
    // 可用优惠券去重
    List<CouponVo> canUseCouponUniqueList = canUseCouponList.stream().collect(collectingAndThen(toCollection(() -> new TreeSet<>(comparingLong(CouponVo::getId))), ArrayList::new));
    // 不可用优惠券
    List<CouponVo> notCanUseCouponList = allCouponUniqueList.stream().filter(
    allCoupon -> canUseCouponUniqueList.stream().noneMatch(canUseCoupon -> Objects.equals(allCoupon.getId(), canUseCoupon.getId())))
    .collect(Collectors.toList());

     
  • 相关阅读:
    集合框架整理及之间的区别
    ArrayList和LinkedList
    GC(Garbage Collection)
    Java常用工具类
    Java异常处理
    JDK环境配置
    内部类总结
    Java字符串定义及常用方法
    Java面向对象总结
    Java数组定义及方法
  • 原文地址:https://www.cnblogs.com/wlong-blog/p/14276123.html
Copyright © 2011-2022 走看看