因此,讓我們開始吧。 有時,您會發現需要確定一個集合中的哪些元素存在于另一個集合中,哪些是常見的和/或哪些不存在于另一個集合中。 阿帕奇百科全書集合在CollectionUtils一些一些實用方法是有用的,尤其是路口 (),但這個帖子去有點超出了到計算集合的集合獨特的元素,它總是很高興坐下來的細節。 我們還將通過支持任意數量的集合(而不是像CollectionUtils那樣僅支持兩個集合)來使解決方案更加通用。 另外,事實是,并非所有人都選擇或能夠包含庫只是為了獲得幾個有用的實用程序方法。
當僅處理兩個集合時,這不是一個難題,但是并非所有開發人員都熟悉java.util.Collection定義的所有方法,因此這里是一些示例代碼。 他們的關鍵是一起使用keepAll和removeAll方法來構建三個集合-通用,僅在集合A中存在,僅在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]
處理三個或更多集合
處理兩個以上的集合時,這個問題有些棘手,但是可以很簡單地以一種通用的方式解決,如下所示:
計算通用元素
計算公共元素很容易,即使有大量集合,此代碼也將始終如一地執行。
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]
計算獨特元素
計算唯一元素與計算通用元素一樣簡單。 請注意,此代碼的性能將隨著您添加大量集合而降低,盡管在大多數實際情況下都沒有關系。 我猜想有一些方法可以優化它,但是由于我沒有遇到問題,所以我沒有打擾tryin。 正如克努斯(Knuth)著名的說法 :“我們應該忘記效率低下的問題,大約有97%的時間是這樣:過早的優化是萬惡之源”。
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]]
這里的所有都是它的。 隨意使用此代碼,無論您喜歡什么,如果有任何改進或建議,請發表評論。 當我們分享知識和經驗時,開發人員都將從中受益。
參考: 計算多個集合中的公共和唯一元素– Java和我們的JCG合作伙伴 ? Carfey軟件博客博客上的Craig Flichel。
翻譯自: https://www.javacodegeeks.com/2012/03/common-and-unique-elements-in-multiple.html