获得两组之间的差异 [英] Getting the difference between 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);
有没有办法比较它们,只返回一组4和5?
Is there a way to compare them and only have a set of 4 and 5 returned?
推荐答案
试试这个
test2.removeAll(test1);
从此集合中删除其所有元素包含在指定集合中(可选操作)。如果指定的集合也是一个集合,则此操作会有效地修改此集合,使其值为两个集合的非对称集合差异。
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.
这篇关于获得两组之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文