def a = ['a','b','c','c','c'] // diff is [b, c, c]
def b = ['a','d','c'] // diff is [d]
// for quick comparison
assert (a.sort() == b.sort()) == false
// to get the differences, remove the intersection from both
a.intersect(b).each{a.remove(it);b.remove(it)}
assert a == ['b','c','c']
assert b == ['d']
assert (a + b) == ['b','c','c','d'] // all diffs
4条答案
按热度按时间toe950271#
我只是使用算术运算符,我认为发生了什么要明显得多:
igsr9ssn2#
集合交集可能会帮助您做到这一点,即使它是一个有点棘手的逆转它。也许是这样的:
kmbjn2e33#
我假设操作员正在请求两个列表之间的exclusive disjunction。
(**注意:**之前的两个解决方案都不处理重复项!)
如果您希望自己在Groovy中对其进行编码,请执行以下操作:
使用整数列表/数组时需要注意的一点是。您可能会因为多态方法
remove(int)
与remove(Object)
而出现问题。See here for a (untested) solution。而不是重新发明轮子,只需要使用一个库(例如
commons-collections
):kyxcudwk4#
If it is a list of numbers, you can do this: