服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - Java两个List<T> 求交集,差集,并集,去重后的并集

Java两个List<T> 求交集,差集,并集,去重后的并集

2022-11-09 11:47Sonder、Aiden Java教程

本文主要介绍了Java两个List<T> 求交集,差集,并集,去重后的并集,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

操作的List<T>声明

oldList

?
1
2
3
4
5
List<String> oldList = new ArrayList<>();
oldList.add("a");
oldList.add("b");
oldList.add("c");
System.out.println("oldList ## " + oldList);

newList

?
1
2
3
4
5
List<String> newList = new ArrayList<>();
newList.add("a");
newList.add("1");
newList.add("2");
System.out.println("newList ## " + newList);

求交集 retainAll()

1. oldList 与 newList 的交集

?
1
2
oldList.retainAll(newList);
System.out.println("求交集 ## " + oldList);

响应

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

2. newList 与 oldList 的交集

?
1
2
newList.retainAll(oldList);
System.out.println("求交集 ## " + newList);

响应

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

求差集 removeAll()

1. oldList 与 newList 的差集

?
1
2
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 的差集

?
1
2
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() 的源码

?
1
2
3
4
public boolean removeAll(Collection<?> c) {
    Objects.requireNonNull(c);
    return batchRemove(c, false);
}

batchRemove的源码

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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 的并集

?
1
2
oldList.addAll(newList);
System.out.println("求并集 ##" + oldList);

响应

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

2. newList 与 oldList 的并集

?
1
2
newList.addAll(oldList);
System.out.println("求并集 ##" + newList);

响应

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

求去重后的并集 

1. oldList 与 newList 去重后的并集

?
1
2
3
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 去重后的并集

?
1
2
3
newList.removeAll(oldList);
newList.addAll(oldList);
System.out.println("求并集 ## " + newList);

响应

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

到此这篇关于Java两个List<T> 求交集,差集,并集,去重后的并集的文章就介绍到这了,更多相关Java List求交集,差集,并集,去重后的并集内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/weixin_42166505/article/details/122627025

延伸 · 阅读

精彩推荐