2009-01-20 57 views
1

我有一個「Set」,我需要使用findAll閉包。該集合包含對象,而不僅僅是原始值。例如......我有一組Employee對象,我需要通過Employee的屬性遍歷並獲取該Empolyee對象的元素。在Groovy中使用findAll Collection Closure

由於某些原因,findAll閉包似乎只是忽略了我的close並返回一個空集。

這是我的語法;

dstCollection = srcCollection.findAll{ 
    it.age == 22 
} 

任何幫助將不勝感激。

謝謝。

回答

3

對我來說,以下工作:

class Employee { 
    int age 

    String toString() { 
     age 
    } 
} 

def list = [ new Employee(age:22), new Employee(age:23), new Employee(age:22) ] as Set 

println list 
println list.findAll { it.age == 22 } 

輸出:

[22, 23, 22] 
[22, 22] 

您可以發佈您的代碼?

編輯:添加「作爲集」,因爲我注意到問題是關於集而不是列表。這也適用於我。

+0

您在toString定義中缺少「{」;-) – 2009-01-20 20:24:15