java8 实现List对象去重

该博客介绍了如何使用Java 8的Stream API对优惠券进行去重操作,包括全部优惠券的去重和可用优惠券的去重。通过创建TreeSet并利用comparingLong方法比较优惠券ID来实现唯一性,然后将结果转换回ArrayList。同时,还展示了如何从全部优惠券中筛选出不可用的优惠券,即排除掉可用优惠券集合中的ID。
摘要由CSDN通过智能技术生成

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

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值