-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathCollectionsSymDiff.java
More file actions
18 lines (14 loc) · 566 Bytes
/
Copy pathCollectionsSymDiff.java
File metadata and controls
18 lines (14 loc) · 566 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/*
Ðåàëèçóéòå ìåòîä, âû÷èñëÿþùèé ñèììåòðè÷åñêóþ ðàçíîñòü äâóõ ìíîæåñòâ.
Ìåòîä äîëæåí âîçâðàùàòü ðåçóëüòàò â âèäå íîâîãî ìíîæåñòâà. Èçìåíÿòü ïåðåäàííûå â íåãî ìíîæåñòâà íå äîïóñêàåòñÿ.
Ïðèìåð
Ñèììåòðè÷åñêàÿ ðàçíîñòü ìíîæåñòâ {1, 2, 3} è {0, 1, 2} ðàâíà {0, 3}.
*/
public static <T> Set<T> symmetricDifference(Set<? extends T> set1, Set<? extends T> set2) {
Set<T> symmetricDiff = new HashSet<T>(set1);
symmetricDiff.addAll(set2);
Set<T> tmp = new HashSet<T>(set1);
tmp.retainAll(set2);
symmetricDiff.removeAll(tmp);
return symmetricDiff;
}