Groovy 查找列表的值
我有两张单子Groovy 查找列表的值,groovy,Groovy,我有两张单子 def flagList = SystemFlag.list() 它包含一个表的域对象 我有另一个使用查询创建的列表。此列表对象中的一个参数包含在flagList中。如何查找第二个列表中是否存在FlagList的id 我可以用普通java实现,但我需要使用Groovy来实现这一点。我发现它使用的是 def it = itemtofindsomehow list.findIndexof { iterator -> iterator.domain.id == it.id
def flagList = SystemFlag.list()
它包含一个表的域对象
我有另一个使用查询创建的列表。此列表对象中的一个参数包含在flagList中。如何查找第二个列表中是否存在FlagList的id
我可以用普通java实现,但我需要使用Groovy来实现这一点。我发现它使用的是
def it = itemtofindsomehow
list.findIndexof { iterator ->
iterator.domain.id == it.id
}
如果我理解正确,您会遇到这种情况:
def listeOne = [1,2,3,4,5]
def listTwo = [2,5,1]
您想查看“listTwo”的“2”是否在“listOne”中
查找特定值:
def found = 2 in listTwo //returns a boolean of the interger 2 is in listTwo
搜索两个列表的公共值:
def intersectionsList = listOne.intersect(listTwo) //gives you a list of value that are in BORTH list
您也可以这样迭代:
listTwo.each { value ->
if(value in listOne) println value //or do something lese
}
或者:
listTwo.each { value ->
listOne.find {value}?.toString() //you can perform an action on the object found in listOne. using '?.' will make sure no nullpointer will be thrown if there is no result.
}
你能把这句话换成一个问题吗?示例列表和您想要的输出将对理解…+1到@tim_yates有很大帮助。听起来您可以通过方法调用使其工作。但是,听起来您正在使用Grails及其域对象,这可能不是一个好答案,因为Grails提供了一些方法来测试数据库中是否存在具有给定属性的对象,而不必检索该类的所有实例。