How can I compare the items in two lists and create a new list with the difference in Groovy?
A:
Collections intersect might help you with that even if it is a little tricky to reverse it. Maybe something like this:
def collection1 = ["test", "a"]
def collection2 = ["test", "b"]
def commons = collection1.intersect(collection2)
def difference = collection1.plus(collection2)
difference.removeAll(commons)
assert ["a", "b"] == difference
Daff
2010-03-30 10:00:09
+4
A:
I'd just use the arithmetic operators, I think it's much more obvious what's going on:
def a = ["foo", "bar", "baz", "baz"]
def b = ["foo", "qux"]
assert ["bar", "baz", "baz", "qux"] == ((a - b) + (b - a))
Ted Naleid
2010-03-31 00:33:50