-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