我有以下功能,我看到人们使用这些功能来获得 两个列表之间的差异。
注意:这不是实际的代码,而是我所拥有的代码的表示,实际上每个数字都可以替换为具有属性的对象。
def listA = [1,2,3,4,5]
def listB = [1,2,3,6,7]
def common = listA.intersect(listB)//expected: [1,2,3], actual: same
def all = listA.plus(listB)//expected: [1,1,2,2,3,3,4,5,6,7], actual: same
all.removeAll(common)//expected: [4,5,6,7], actual: [6,7]
简化,
def big = [1, 1, 2, 3]
def small = [1, 2]
代码
big.removeAll(small)
删除所有匹配元素。
1
中的 1
和 big
都与 1
中的 small
匹配,因此它们都被删除,结果为 [3]
。您可能想要的是
small.each { big.removeElement(it) }
对于
small
的每个元素,删除 big
的 单个匹配元素,得到结果
[1, 3]
。
请参阅文档。