最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java兩個List<T> 求交集,差集,并集,去重后的并集

 更新時間:2022年04月08日 10:11:44   作者:Sonder、Aiden  
本文主要介紹了Java兩個List<T> 求交集,差集,并集,去重后的并集,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

操作的List<T>聲明

oldList

List<String> oldList = new ArrayList<>();
oldList.add("a");
oldList.add("b");
oldList.add("c");
System.out.println("oldList ## " + oldList);

newList

List<String> 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的源碼

(如果傳的第二個參數(shù)是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]

到此這篇關于Java兩個List<T> 求交集,差集,并集,去重后的并集的文章就介紹到這了,更多相關Java List求交集,差集,并集,去重后的并集內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論

夏邑县| 昌都县| 古浪县| 高邮市| 柳州市| 广汉市| 台江县| 沙河市| 宽城| 鄯善县| 资溪县| 米脂县| 大化| 和静县| 攀枝花市| 邢台市| 土默特右旗| 科技| 大名县| 靖边县| 龙门县| 苏州市| 东乡族自治县| 泰和县| 潜山县| 天气| 忻城县| 阿城市| 和政县| 宁南县| 调兵山市| 北川| 宁德市| 福建省| 宁都县| 大关县| 北票市| 阳春市| 福海县| 福州市| 湖南省|