如何比较 Groovy 中的两个列表 [英] How can I compare two lists in Groovy

查看:37
本文介绍了如何比较 Groovy 中的两个列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何比较两个列表中的项目并创建一个具有 Groovy 差异的新列表?

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

推荐答案

Collections intersect 可能会帮助你解决这个问题,即使反转它有点棘手.也许是这样的:

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

这篇关于如何比较 Groovy 中的两个列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆