集合去重,高效算法

我们最常用的两个集合去重的方法是removeAll,但是当两个集合达到上万之后就已经很慢了,上百万之后,处理速度更是令人难以忍受,处理时间超过10分钟以上,测试代码如下:

public class Test {
    public static void main(String[] args) {
        List<Integer> list1 = new ArrayList<>();
        List<Integer> list2 = new ArrayList<>();
        for(int i = 0; i<=1000000; i++) {
            list1.add(i);
        }
        for(int i = 500000; i<=1500000; i++) {
            list2.add(i);
        }
        System.out.println("start: " + new Date());
        list2.removeAll(list1);
        System.out.println("end: " + new Date());
    }
}

这个时候我们可以使用CollectionUtils的intersection方法(取交集)和disjunction方法(取差集),disjunction 就是我们要的去重之后的集合,代码如下:

public class Test {
    public static void main(String[] args) {
        List<Integer> list1 = new ArrayList<>();
        List<Integer> list2 = new ArrayList<>();
        for(int i = 0; i<=1000000; i++) {
            list1.add(i);
        }
        for(int i = 500000; i<=1500000; i++) {
            list2.add(i);
        }
        System.out.println("start: " + new Date());
        Collection intersection = CollectionUtils.intersection(list1, list2);
        Collection disjunction = CollectionUtils.disjunction(list2, intersection);
        System.out.println("end: " + new Date());
    }
}

测试结果:
start: Thu Dec 06 20:36:11 CST 2018
end: Thu Dec 06 20:36:12 CST 2018
单个集合数据量达到100万,处理时间为1s,结果显而易见。

源码如下:

public static Collection intersection(Collection a, Collection b) {
        ArrayList list = new ArrayList();
        Map mapa = getCardinalityMap(a);
        Map mapb = getCardinalityMap(b);
        Set elts = new HashSet(a);
        elts.addAll(b);
        Iterator it = elts.iterator();

        while(it.hasNext()) {
            Object obj = it.next();
            int i = 0;

            for(int m = Math.min(getFreq(obj, mapa), getFreq(obj, mapb)); i < m; ++i) {
                list.add(obj);
            }
        }

        return list;
    }


    public static Collection disjunction(Collection a, Collection b) {
        ArrayList list = new ArrayList();
        Map mapa = getCardinalityMap(a);
        Map mapb = getCardinalityMap(b);
        Set elts = new HashSet(a);
        elts.addAll(b);
        Iterator it = elts.iterator();

        while(it.hasNext()) {
            Object obj = it.next();
            int i = 0;

            for(int m = Math.max(getFreq(obj, mapa), getFreq(obj, mapb)) - Math.min(getFreq(obj, mapa), getFreq(obj, mapb)); i < m; ++i) {
                list.add(obj);
            }
        }

        return list;
    }

    public static Map getCardinalityMap(Collection coll) {
        Map count = new HashMap();
        Iterator it = coll.iterator();

        while(it.hasNext()) {
            Object obj = it.next();
            Integer c = (Integer)((Integer)count.get(obj));
            if (c == null) {
                count.put(obj, INTEGER_ONE);
            } else {
                count.put(obj, new Integer(c + 1));
            }
        }

        return count;
    }


从源码不难发现,集合遍历次数仅为集合a和集合b的并集的数量,通过Map的使用极大的提高了处理速度。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值