Java中两个集合的对称差
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9698652/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Symmetric difference of two sets in Java
提问by Abhij
There are two TreeSet
s in my app:
TreeSet
我的应用程序中有两个:
set1 = {501,502,503,504}
set2 = {502,503,504,505}
I want to get the symmetric differenceof these sets so that my output would be the set:
我想得到这些集合的对称差异,以便我的输出是集合:
set = {501,505}
采纳答案by Donal Fellows
You're after the symmetric difference. This is discussed in the Java tutorial.
你在追求对称差异。这在Java 教程中进行了讨论。
Set<Type> symmetricDiff = new HashSet<Type>(set1);
symmetricDiff.addAll(set2);
// symmetricDiff now contains the union
Set<Type> tmp = new HashSet<Type>(set1);
tmp.retainAll(set2);
// tmp now contains the intersection
symmetricDiff.removeAll(tmp);
// union minus intersection equals symmetric-difference
回答by digitebs
use retain all,remove all then addAll to do a union of existing set.
使用retain all,remove all 然后addAll 来做现有集合的并集。
- intersectionSet.retainAll(set2) // intersectionSet is a copy of set1
- set1.addAll(set2); // do a union of set1 and set2
- then remove the duplicates set1.removeAll(intersectionSet);
- 交集集.retainAll(集2) //交集集是集1的副本
- set1.addAll(set2); // 对 set1 和 set2 进行并集
- 然后删除重复项 set1.removeAll(intersectionSet);
回答by Chandra Sekhar
Set<String> s1 = new HashSet<String>();
Set<String> s2 = new HashSet<String>();
s1.add("a");
s1.add("b");
s2.add("b");
s2.add("c");
Set<String> s3 = new HashSet<String>(s1);
s1.removeAll(s2);
s2.removeAll(s3);
s1.addAll(s2);
System.out.println(s1);
output of s1 : [a,c]
s1 的输出:[a,c]
回答by beny23
You could use CollectionUtils#disjunction
你可以用 CollectionUtils#disjunction
EDIT:
编辑:
Alternatively with less pre-Java-5-ness, use Guava Sets#symmetricDifference
或者,使用较少的前 Java-5-ness,使用 Guava Sets#symmetricDifference
回答by jameshfisher
Those looking for set subtraction/complement(not symmetric difference/disjunction) can use CollectionUtils.subtract(a,b)
or Sets.difference(a,b)
.
那些寻找集合减法/补码(非对称差/析取)的人可以使用CollectionUtils.subtract(a,b)
或Sets.difference(a,b)
。
回答by Donald Raab
You could try Sets.symmetricDifference()
from Eclipse Collections.
您可以Sets.symmetricDifference()
从Eclipse Collections尝试。
Set<Integer> set1 = new TreeSet<>(Arrays.asList(501,502,503,504));
Set<Integer> set2 = new TreeSet<>(Arrays.asList(502,503,504,505));
Set<Integer> symmetricDifference =
Sets.symmetricDifference(set1, set2);
Assert.assertEquals(
new TreeSet<>(Arrays.asList(501, 505)),
symmetricDifference);
Note: I am a committer for Eclipse Collections.
注意:我是 Eclipse Collections 的提交者。
回答by dobrivoje
if we use package com.google.common.collect, we may ellegantly find symmetric difference like this :
如果我们使用 com.google.common.collect 包,我们可能会优雅地找到这样的对称差异:
Set<Integer> s1 = Stream.of( 1,2,3,4,5 ).collect( Collectors.toSet());
Set<Integer> s2 = Stream.of( 2,3,4 ).collect( Collectors.toSet());
System.err.println(Sets.symmetricDifference( s1,s2 ));
The output will be : [1, 5]
输出将是:[1, 5]