-1
的价值,我有两个列表查找列表
def flagList = SystemFlag.list()
这包含一个表
的域对象我有我创建使用查询另一个列表。该列表对象中的一个参数包含在flagList中。我如何才能找到第二个列表中是否存在FlagList的ID?
我能做到这一点在普通的Java,但我需要使用Groovy这一点。
的价值,我有两个列表查找列表
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 to @tim_yates。这听起来,你可能能够使其与A ['find']工作(http://groovy.codehaus.org/groovy-jdk/java/util/Collection.html#find%28groovy.lang.Closure%29 )方法调用。但是,因为它也听起来是你正在使用Grails及其域对象,所以这可能不是一个好的答案,因为Grails提供了一种方法来测试数据库上是否存在具有给定属性的对象,而无需检索所有的实例类。 – epidemian
再来? * _ * – sriram