栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

两个List<T> 之间求:交集、差集、并集、去重后的并集

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

两个List<T> 之间求:交集、差集、并集、去重后的并集

注:文章皆为个人纪录,可用性请以最终结果为准,若有错还请大佬们指出,谢谢!

操作的List声明 oldList
List oldList = new ArrayList<>();
oldList.add("a");
oldList.add("b");
oldList.add("c");
System.out.println("oldList ## " + oldList);
newList
List newList = new ArrayList<>();
newList.add("a");
newList.add("1");
newList.add("2");
System.out.println("newList ## " + newList);
求交集 retainAll() 1. oldList 与 newList 的交集
oldList.retainAll(newList);
System.out.println("求交集 ## " + oldList);

响应

oldList ## [a, b, c]
newList ## [a, 1, 2]
求交集 ## [a]
2. newList 与 oldList 的交集
newList.retainAll(oldList);
System.out.println("求交集 ## " + newList);

响应

oldList ## [a, b, c]
newList ## [a, 1, 2]
求交集 ## [a]
求差集 removeAll() 1. oldList 与 newList 的差集
oldList.removeAll(newList);
System.out.println("oldList.removeAll(newList);  --> 求差集( 在 oldList 集合中,删掉 oldList 与 newList 中都有的元素): ## " + oldList);

响应

oldList ## [a, b, c]
newList ## [a, 1, 2]
oldList.removeAll(newList);  --> 求差集( 在 oldList 集合中,删掉 oldList 与 newList 中都有的元素): ## [b, c]
2. newList与 oldList 的差集
newList.removeAll(oldList);
System.out.println("newList.removeAll(oldList);  --> 求差集( 在 newList 集合中,删掉 oldList 与 newList 中都有的元素): ## " + newList);

响应

oldList ## [a, b, c]
newList ## [a, 1, 2]
newList.removeAll(oldList);  --> 求差集( 在 newList 集合中,删掉 oldList 与 newList 中都有的元素): ## [1, 2]
3. 源码解析 removeAll() 的源码
    public boolean removeAll(Collection c) {
        Objects.requireNonNull(c);
        return batchRemove(c, false);
    } 
batchRemove的源码

(如果传的第二个参数是false,保留差集;如果传的是true,保留的是交集)

    private boolean batchRemove(Collection c, boolean complement) {
        final Object[] elementData = this.elementData;
        int r = 0, w = 0;
        boolean modified = false;
        try {
            for (; r < size; r++)
                if (c.contains(elementData[r]) == complement)
                    elementData[w++] = elementData[r];
        } finally {
            // Preserve behavioral compatibility with AbstractCollection,
            // even if c.contains() throws.
            if (r != size) {
                System.arraycopy(elementData, r,
                                 elementData, w,
                                 size - r);
                w += size - r;
            }
            if (w != size) {
                // clear to let GC do its work
                for (int i = w; i < size; i++)
                    elementData[i] = null;
                modCount += size - w;
                size = w;
                modified = true;
            }
        }
        return modified;
    }
求并集 addAll() 1. oldList 与 newList 的并集
oldList.addAll(newList);
System.out.println("求并集 ##" + oldList);

响应

oldList ## [a, b, c]
newList ## [a, 1, 2]
求并集 ##[a, b, c, a, 1, 2]
2. newList 与 oldList 的并集
newList.addAll(oldList);
System.out.println("求并集 ##" + newList);

响应

oldList ## [a, b, c]
newList ## [a, 1, 2]
求并集 ##[a, 1, 2, a, b, c]
求去重后的并集  1. oldList 与 newList 去重后的并集
oldList.removeAll(newList); // 先求差集得到去除相同项后的 oldList
oldList.addAll(newList); // 再求并集
System.out.println("求并集 ## " + oldList);

响应

oldList ## [a, b, c]
newList ## [a, 1, 2]
求并集 ## [b, c, a, 1, 2]
2. newList与 oldList 去重后的并集
newList.removeAll(oldList);
newList.addAll(oldList);
System.out.println("求并集 ## " + newList);

响应

oldList ## [a, b, c]
newList ## [a, 1, 2]
求并集 ## [1, 2, a, b, c]

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/713100.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号