So if I have two sets:
所以,如果我有两套:
Set<int> test1 = new HashSet<Integer>();
test1.add(1);
test1.add(2);
test1.add(3);
Set<int> test2 = new HashSet<Integer>();
test2.add(1);
test2.add(2);
test2.add(3);
test2.add(4);
test2.add(5);
Is there a way to compare them and only have a set of 4 and 5 returned?
有没有办法比较它们,只有一组4和5返回?
4 个解决方案
#1
125
Try this
尝试这个
test2.removeAll(test1);
集#的removeAll
Removes from this set all of its elements that are contained in the specified collection (optional operation). If the specified collection is also a set, this operation effectively modifies this set so that its value is the asymmetric set difference of the two sets.
从此集合中删除指定集合中包含的所有元素(可选操作)。如果指定的集合也是一个集合,则此操作会有效地修改此集合,以使其值为两个集合的不对称集合差异。
#2
84
If you use Guava (former Google Collections) library there is a solution:
如果您使用Guava(以前的Google Collections)库,则有一个解决方案:
SetView<Number> difference = com.google.common.collect.Sets.difference(test2, test1);
The returned SetView
is a Set
, it is a live representation you can either make immutable or copy to another set. test1
and test2
are left intact.
返回的SetView是一个Set,它是一个实时表示,您可以使其不可变或复制到另一个集合。 test1和test2保持不变。
#3
11
Yes:
是:
test2.removeAll(test1)
Although this will mutate test2
, so create a copy if you need to preserve it.
虽然这会改变test2,但如果需要保留它,请创建一个副本。
Also, you probably meant <Integer>
instead of <int>
.
此外,您可能需要
#4
5
If you are using Java 8, you could try something like this:
如果您使用的是Java 8,可以尝试这样的方法:
public Set<Number> difference(final Set<Number> set1, final Set<Number> set2){
final Set<Number> larger = set1.size() > set2.size() ? set1 : set2;
final Set<Number> smaller = larger.equals(set1) ? set2 : set1;
return larger.stream().filter(n -> !smaller.contains(n)).collect(Collectors.toSet());
}
#1
125
Try this
尝试这个
test2.removeAll(test1);
集#的removeAll
Removes from this set all of its elements that are contained in the specified collection (optional operation). If the specified collection is also a set, this operation effectively modifies this set so that its value is the asymmetric set difference of the two sets.
从此集合中删除指定集合中包含的所有元素(可选操作)。如果指定的集合也是一个集合,则此操作会有效地修改此集合,以使其值为两个集合的不对称集合差异。
#2
84
If you use Guava (former Google Collections) library there is a solution:
如果您使用Guava(以前的Google Collections)库,则有一个解决方案:
SetView<Number> difference = com.google.common.collect.Sets.difference(test2, test1);
The returned SetView
is a Set
, it is a live representation you can either make immutable or copy to another set. test1
and test2
are left intact.
返回的SetView是一个Set,它是一个实时表示,您可以使其不可变或复制到另一个集合。 test1和test2保持不变。
#3
11
Yes:
是:
test2.removeAll(test1)
Although this will mutate test2
, so create a copy if you need to preserve it.
虽然这会改变test2,但如果需要保留它,请创建一个副本。
Also, you probably meant <Integer>
instead of <int>
.
此外,您可能需要
#4
5
If you are using Java 8, you could try something like this:
如果您使用的是Java 8,可以尝试这样的方法:
public Set<Number> difference(final Set<Number> set1, final Set<Number> set2){
final Set<Number> larger = set1.size() > set2.size() ? set1 : set2;
final Set<Number> smaller = larger.equals(set1) ? set2 : set1;
return larger.stream().filter(n -> !smaller.contains(n)).collect(Collectors.toSet());
}