How can I compare two lists in Groovy

后端 未结 4 1592
北海茫月
北海茫月 2020-12-15 04:33

How can I compare the items in two lists and create a new list with the difference in Groovy?

4条回答
  •  星月不相逢
    2020-12-15 05:20

    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
    

提交回复
热议问题