集合 求交集 求差集 高效 算法 性能对比 与 分析 JAVA

问题:

两个集合,分别含有一定数量的元素,如何快速得到两个集合的交集和差集?

举例:

给定两个集合List<String> list1和List<String> list2,假定两个集合分别具有m和n个元素,要求取出两个集合中不同的元素,A比B多的元素和B比A多的元素。

说明:

1.以String作为集合中元素的类型,如果是自定义的数据结构,需要重写equals方法

2.输入参数:第一个集合list1,第二个集合list2

3.方法:实现求两个集合交集的方法 & 实现求第一个输入参数集合与第二个输入参数集合的正向差(list1-list2)【即第一个集合减去出现在第二个集合中的元素,相当于list1-commonList】

4.输出参数:交集/差集的集合结果

实现:

方法一:遍历两个集合

实现求 list1-list2:

    private static List<String> getDiffrent(List<String> list1, List<String> list2) {
        List<String> different = new ArrayList<String>();
        for (String str : list1) {
            if (!list2.contains(str)) {
                different.add(str);
            }
        }
        return different;
    }

实现求交集:

    private static List<String> getCommon(List<String> list1, List<String> list2) {
        List<String> common = new ArrayList<String>();
        for (String str : list1) {
            if (list2.contains(str)) {
                common.add(str);
            }
        }
        return common;
    }

分析:利用 List 自带的 contains 方法逐个判断另一个集合中的元素是否属于这个集合,总共要循环的次数是两个 List 的元素数量相乘的积,因此时间复杂度为 O(m*n) ~ O(n^2) ,空间复杂度为 O(m+n) ~ O(1) 。 

ArrayList 的 contains 方法使用遍历的方法进行判断,源码如下:


    /**
     * Returns <tt>true</tt> if this list contains the specified element.
     * More formally, returns <tt>true</tt> if and only if this list contains
     * at least one element <tt>e</tt> such that
     * <tt>(o==null&nbsp;?&nbsp;e==null&nbsp;:&nbsp;o.equals(e))</tt>.
     *
     * @param o element whose presence in this list is to be tested
     * @return <tt>true</tt> if this list contains the specified element
     */
    public boolean contains(Object o) {
        return indexOf(o) >= 0;
    }

    /**
     * Returns the index of the first occurrence of the specified element
     * in this list, or -1 if this list does not contain the element.
     * More formally, returns the lowest index <tt>i</tt> such that
     * <tt>(o==null&nbsp;?&nbsp;get(i)==null&nbsp;:&nbsp;o.equals(get(i)))</tt>,
     * or -1 if there is no such index.
     */
    public int indexOf(Object o) {
        if (o == null) {
            for (int i = 0; i < size; i++)
                if (elementData[i]==null)
                    return i;
        } else {
            for (int i = 0; i < size; i++)
                if (o.equals(elementData[i]))
                    return i;
        }
        return -1;
    }

其中 elementData 为 ArrayList 中存储元素的数组成员变量,使用 indexOf 方法确定重复元素在 ArrayList 中的位置 i ,通过判断 i 返回一个 Boolean 结果。

方法二:采用 List 提供的 retainAll() 和 removeAll() 方法

retainAll() 方法用于保留 arraylist 中在指定集合中也存在的那些元素,也就是删除指定集合中不存在的那些元素。retainAll() 方法的语法为:

arraylist.retainAll(Collection c);

注:arraylist 是 ArrayList 类的一个对象。

removeAll() 方法用于删除存在于指定集合中的动态数组元素。removeAll() 方法的语法为:

arraylist.removeAll(Collection c);

注:arraylist 是 ArrayList 类的一个对象。

因此,使用上述两个函数,我们可以得到求A-B(差集)和交集的方法:

实现求 list1-list2:

    private static List<String> getDiffrent2(List<String> list1, List<String> list2) {
        List<String> different = new ArrayList<String>(list1);
        different.removeAll(list2);
        return different;
    }

实现求交集: 

    private static List<String> getCommon2(List<String> list1, List<String> list2) {
        List<String> common = new ArrayList<String>(list1);
        common.retainAll(list2);
        return common;
    }

分析:同理,这两个方法函数内部也是使用循环进行比较和处理的,因此总共要循环的次数还是两个 List 的元素数量相乘的积,时间复杂度为 O(m*n) ~ O(n^2) ,空间复杂度为 O(m+n) ~ O(1) 。 

retainAll() 和 removeAll() 方法的实现如下:


    /**
     * Removes from this list all of its elements that are contained in the
     * specified collection.
     *
     * @param c collection containing elements to be removed from this list
     * @return {@code true} if this list changed as a result of the call
     * @throws ClassCastException if the class of an element of this list
     *         is incompatible with the specified collection
     * (<a href="Collection.html#optional-restrictions">optional</a>)
     * @throws NullPointerException if this list contains a null element and the
     *         specified collection does not permit null elements
     * (<a href="Collection.html#optional-restrictions">optional</a>),
     *         or if the specifi
  • 4
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值