对象快速去重

场景:今日boss叫我把一个包含数千条数据的Excel表格导入数据库,要求去除重复数据,并保证性能。其中有两个字段可以判断该条数据是否唯一。
需求其实不难,使用工具,如ExcelUtil将数据导入,封装个成对象,通过两个字段进行比较,判断唯一,再存入集合中批量插入。
然而,在对象唯一性校验时,我使用了map集合的方式,虽然效果出来了,但是越看越觉得这个代码low。(如下是省略导入步骤,只做唯一校验)

        Map map = new HashMap<> ();
        HappyPupy happyPupy = new HappyPupy ();
        happyPupy.setAge (18);
        happyPupy.setName ("张三");
        happyPupy.setScore ("50");
        happyPupy.setTile ("小学");
        map.put (happyPupy.getName ()+happyPupy.getScore (),happyPupy);
        
        HappyPupy happyPupy1 = new HappyPupy ();
        happyPupy1.setAge (20);
        happyPupy1.setName ("张四");
        happyPupy1.setScore ("60");
        happyPupy1.setTile ("中学");
        map.put (happyPupy1.getName ()+happyPupy1.getScore (),happyPupy1);

        HappyPupy happyPupy2 = new HappyPupy ();
        happyPupy2.setAge (20);
        happyPupy2.setName ("张四");
        happyPupy2.setScore ("60");
        happyPupy2.setTile ("中学");
        map.put (happyPupy2.getName ()+happyPupy2.getScore (),happyPupy2);

        HappyPupy happyPupy3 = new HappyPupy ();
        happyPupy3.setAge (22);
        happyPupy3.setName ("张四");
        happyPupy3.setScore ("60");
        happyPupy3.setTile ("中学");
        map.put (happyPupy3.getName ()+happyPupy3.getScore (),happyPupy3);

        List<HappyPupy> list = new ArrayList<> ();
        Set<Map.Entry<String, Object>> eSet = map.entrySet ();
        Iterator<Map.Entry<String, Object>> it3 = eSet.iterator ();
        while (it3.hasNext ()) {
            list.add ((HappyPupy) it3.next ().getValue ());
        }
        ...........

其中name和score组合为唯一条件,通过HashMap键不能重复的特性,将两个字段组合作为Key,对象作为值存入到map集合中,再将map集合转成List集合进行批量插入。
其实也没啥问题,功能是实现了,但是左看右看,就是觉得low,于是绞尽脑汁(网上白嫖)做了如下改进。

        HappyPupy happyPupy = new HappyPupy ();
        happyPupy.setAge (18);
        happyPupy.setName ("张三");
        happyPupy.setScore ("50");
        happyPupy.setTile ("小学");

        HappyPupy happyPupy1 = new HappyPupy ();
        happyPupy1.setAge (20);
        happyPupy1.setName ("张四");
        happyPupy1.setScore ("60");
        happyPupy1.setTile ("中学");

        HappyPupy happyPupy2 = new HappyPupy ();
        happyPupy2.setAge (20);
        happyPupy2.setName ("张四");
        happyPupy2.setScore ("60");
        happyPupy2.setTile ("中学");


        HappyPupy happyPupy3 = new HappyPupy ();
        happyPupy3.setAge (22);
        happyPupy3.setName ("张四");
        happyPupy3.setScore ("60");
        happyPupy3.setTile ("中学");
        List<HappyPupy> read = new ArrayList<> ();
        read.add (happyPupy);
        read.add (happyPupy1);
        read.add (happyPupy2);
        read.add (happyPupy3);

        //去重后的数据集合
        ArrayList<HappyPupy> collect = read.parallelStream().
                collect(Collectors.collectingAndThen(
                        Collectors.toCollection(() -> new TreeSet<> (Comparator.comparing(o -> o.getName () +":" + o.getAge ()))), ArrayList::new));
  ..........                      

这样子的代码是不是要简洁很多。如有更加优秀的方法,欢迎评论区讨论。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值