stream去重

场景

  • 对list去重本来使用set转换下是快速的方式,但是需要重新class的equals和hashCode,equals根据需要对比的数据重新一遍就行了,但是equals需要保证相同的特定属性得到相同的值,实现的方式简单 但是需要考虑效率 。这个效率拿不准就需要一个新的去重方式了

实现

  1. Collectors.collectingAndThen在进行归纳动作结束之后,对归纳的结果进行二次处理。

templateList.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(item->item.getShopId() + item.getKid()))), ArrayList::new));

  1. 举例
        List<TradeExternalAuthTemplate> templateList = new ArrayList<>();
        TradeExternalAuthTemplate template = new TradeExternalAuthTemplate();
        template.setShopId(1);
        template.setKid("kid1");
        template.setAuth("auth");

        templateList.add(template);
        TradeExternalAuthTemplate template2 = new TradeExternalAuthTemplate();
        template2.setShopId(1);
        template2.setKid("kid1");
        template2.setAuth("auth2");
        templateList.add(template2);

        List<TradeExternalAuthTemplate> tradeFilterList = templateList.stream().distinct().collect(Collectors.toList());

        List<TradeExternalAuthTemplate> tradeFilterList2 = templateList.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(item -> item.getShopId()+ item.getKid()))), ArrayList::new));

        System.out.println(tradeFilterList);
        System.out.println(tradeFilterList2);

在这里插入图片描述

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值