日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

js删除两个集合中共同元素_多个集合中的共同和独特元素

發(fā)布時(shí)間:2023/12/3 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 js删除两个集合中共同元素_多个集合中的共同和独特元素 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

js刪除兩個(gè)集合中共同元素

本周,我們將暫時(shí)中斷較高級別的問題和技術(shù)文章,以解決我們中許多人可能面臨的一些代碼問題。 沒什么花哨的或太辛苦的,但是有一天它可能會(huì)節(jié)省您15分鐘的時(shí)間,偶爾回到基礎(chǔ)上也很不錯(cuò)。

因此,讓我們開始吧。 有時(shí),您會(huì)發(fā)現(xiàn)需要確定一個(gè)集合中的哪些元素存在于另一個(gè)集合中,哪些是常見的,和/或哪些不存在于另一個(gè)集合中。 阿帕奇百科全書集合在CollectionUtils一些一些實(shí)用方法是有用的,尤其是路口 (),但這個(gè)帖子去有點(diǎn)超出了到計(jì)算集合的集合獨(dú)特的元素,它總是很高興坐下來的細(xì)節(jié)。 我們還將通過支持任意數(shù)量的集合(而不是像CollectionUtils那樣僅支持兩個(gè)集合)來使解決方案更加通用。 另外,事實(shí)是,并非所有人都選擇或能夠包含庫只是為了獲得一些有用的實(shí)用程序方法。

當(dāng)只處理兩個(gè)集合時(shí),這不是一個(gè)難題,但是并不是所有的開發(fā)人員都熟悉java.util.Collection定義的所有方法,因此這里是一些示例代碼。 他們的關(guān)鍵是一起使用retainAllremoveAll方法來構(gòu)建三個(gè)集合-通用,僅出現(xiàn)在集合A中,僅出現(xiàn)在集合B中。

Set<String> a = new HashSet<String>(); a.add("a"); a.add("a2"); a.add("common");Set<String> b = new HashSet<String>(); b.add("b"); b.add("b2"); b.add("common");Set<String> inAOnly = new HashSet<String>(a); inAOnly.removeAll(b); System.out.println("A Only: " + inAOnly );Set<String> inBOnly = new HashSet<String>(b); inBOnly .removeAll(a); System.out.println("B Only: " + inBOnly );Set<String> common = new HashSet<String>(a); common.retainAll(b); System.out.println("Common: " + common);

輸出:

A Only: [a, a2] B Only: [b, b2] Common: [common1]

處理三個(gè)或更多集合
處理兩個(gè)以上的集合時(shí),這個(gè)問題有些棘手,但是可以很簡單地以通用的方式解決,如下所示:

計(jì)算通用元素
計(jì)算公共元素很容易,并且即使有大量集合,此代碼也將始終如一地執(zhí)行。

public static void main(String[] args) {List<String> a = Arrays.asList("a", "b", "c");List<String> b = Arrays.asList("a", "b", "c", "d");List<String> c = Arrays.asList("d", "e", "f", "g");List<List<String>> lists = new ArrayList<List<String>>();lists.add(a);System.out.println("Common in A: " + getCommonElements(lists));lists.add(b);System.out.println("Common in A & B: " + getCommonElements(lists));lists.add(c);System.out.println("Common in A & B & C: " + getCommonElements(lists));lists.remove(a);System.out.println("Common in B & C: " + getCommonElements(lists)); }public static <T> Set<T> getCommonElements(Collection<? extends Collection<T>> collections) {Set<T> common = new LinkedHashSet<T>();if (!collections.isEmpty()) {Iterator<? extends Collection<T>> iterator = collections.iterator();common.addAll(iterator.next());while (iterator.hasNext()) {common.retainAll(iterator.next());}}return common; }

輸出:

Common in A: [a, b, c] Common in A & B: [a, b, c] Common in A & B & C: [] Common in B & C: [d]

計(jì)算獨(dú)特元素
計(jì)算唯一元素與計(jì)算通用元素一樣簡單。 請注意,此代碼的性能將隨著您添加大量集合而降低,盡管在大多數(shù)實(shí)際情況下都沒有關(guān)系。 我猜想有一些方法可以優(yōu)化它,但是由于我沒有遇到問題,所以我沒有打擾tryin。 正如克努斯(Knuth)著名的說法 :“我們應(yīng)該忘記效率低下的問題,大約有97%的時(shí)間是這樣:過早的優(yōu)化是萬惡之源”。

public static void main(String[] args) {List<String> a = Arrays.asList("a", "b", "c");List<String> b = Arrays.asList("a", "b", "c", "d");List<String> c = Arrays.asList("d", "e", "f", "g");List<List<String>> lists = new ArrayList<List<String>>();lists.add(a);System.out.println("Unique in A: " + getUniqueElements(lists));lists.add(b);System.out.println("Unique in A & B: " + getUniqueElements(lists));lists.add(c);System.out.println("Unique in A & B & C: " + getUniqueElements(lists));lists.remove(a);System.out.println("Unique in B & C: " + getUniqueElements(lists)); }public static <T> List<Set<T>> getUniqueElements(Collection<? extends Collection<T>> collections) {List<Set<T>> allUniqueSets = new ArrayList<Set<T>>();for (Collection<T> collection : collections) {Set<T> unique = new LinkedHashSet<T>(collection);allUniqueSets.add(unique);for (Collection<T> otherCollection : collections) {if (collection != otherCollection) {unique.removeAll(otherCollection);}}}return allUniqueSets; }

輸出:

Unique in A: [[a, b, c]] Unique in A & B: [[], [d]] Unique in A & B & C: [[], [], [e, f, g]] Unique in B & C: [[a, b, c], [e, f, g]]

這里的所有都是它的。 隨意使用此代碼,無論您喜歡什么,如果有任何改進(jìn)或建議,請發(fā)表評論。 當(dāng)我們分享知識和經(jīng)驗(yàn)時(shí),開發(fā)人員都將從中受益。

參考: 計(jì)算多個(gè)集合中的公共和唯一元素– Java和我們的JCG合作伙伴 ? Carfey軟件博客博客上的Craig Flichel。


翻譯自: https://www.javacodegeeks.com/2012/03/common-and-unique-elements-in-multiple.html

js刪除兩個(gè)集合中共同元素

總結(jié)

以上是生活随笔為你收集整理的js删除两个集合中共同元素_多个集合中的共同和独特元素的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。