这个方法借助闭包,统计符合闭包条件元素的个数,返回值为Integer
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']
]
int count = people.count {it.value.age >35}
print count
}
}
输出:
2
网友评论