class GroovyTest {
static void main(args) {
def people = [
1: [name:'Bob', age:32, gender:'M'],
2: [name:'Johnny', age:36, gender:'M'],
3: [name:'Claire', age:21, gender:'F'],
4: [name:'Amy', age:54, gender:'F']
]
def agesOfMales = people.findAll{
id,person -> person.gender == 'M'
}.collect{
id, person -> person.age
}
println agesOfMales
def females = people.findAll{it.value.gender == 'F'}
def ageOfFemales = females.collect{it.value.age}
println ageOfFemales
}
}
输出:
[32, 36]
[21, 54]