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

     
  • 相关阅读:
    zju 2886
    zju 2478
    UVA350-水题
    UVA699-落叶-二叉树
    UVA327
    UVA548
    java环境变量
    synchronized关键字
    uva297
    UVA196
  • 原文地址:https://www.cnblogs.com/wlong-blog/p/14276123.html
Copyright © 2011-2022 走看看