我正在尝试从地图中获取列表并基于id的列表,并希望根据年龄(年龄> 35)检索值(列表)
def people = [
"1": [[name:'Bob', age: 32, gender: 'M'],[name:'Johnny', age: 36, gender: 'M']],
"3": [[name:'Claire', age: 21, gender: 'F'],[name:'Amy', age: 54, gender:'F']],
"4": [[name:'John', age: 41, gender: 'F'],[name:'Sam', age: 54, gender:'F']]
]
def id = ["1","3"]
def age = people.subMap(id).values().findAll{it.age > 35}
但是,我收到以下错误:
Cannot compare java.util.ArrayList with value '[32, 36]' and java.lang.Integer with value '35'
如何获得年龄> 1的所有人的名单? 35?
答案 0 :(得分:1)
您需要UIColor.clear.cgColor
.flatten()
的结果,因为它会生成一系列地图列表:
people.subMap(id).values()
执行[[[name:Bob, age:32, gender:M], [name:Johnny, age:36, gender:M]], [[name:Claire, age:21, gender:F], [name:Amy, age:54, gender:F]]]
时,您将获得地图列表:
people.subMap(id).values().flatten()
然后您可以申请[[name:Bob, age:32, gender:M], [name:Johnny, age:36, gender:M], [name:Claire, age:21, gender:F], [name:Amy, age:54, gender:F]]
.findAll { it.age > 35 }
输出:
def people = [
"1": [[name: 'Bob', age: 32, gender: 'M'], [name: 'Johnny', age: 36, gender: 'M']],
"3": [[name: 'Claire', age: 21, gender: 'F'], [name: 'Amy', age: 54, gender: 'F']],
"4": [[name: 'John', age: 41, gender: 'F'], [name: 'Sam', age: 54, gender: 'F']]
]
def id = ["1", "3"]
def age = people.subMap(id).values().flatten().findAll { it.age > 35 }
println age
希望它有所帮助。
答案 1 :(得分:0)
虽然不像其他答案那样 Groovy ,但是一个简单的方法是collect
通过转换函数,并返回[]
进行不匹配(可以是flattened
出来。
就是这样:
def people = [
"1": [[name:'Bob', age: 32, gender: 'M'],[name:'Johnny', age: 36, gender: 'M']],
"3": [[name:'Claire', age: 21, gender: 'F'],[name:'Amy', age: 54, gender:'F']],
"4": [[name:'John', age: 41, gender: 'F'],[name:'Sam', age: 54, gender:'F']]
]
def targetIds = ["1","3"]
def targetAge = 35
然后:
def findAgeOverN = { def key, def list, def ids, def n ->
(ids.contains(key)) ? list.findAll { it["age"] > n } : []
}
def result = people.collect { key, list -> findAgeOverN(key, list, targetIds, targetAge) }
.flatten()
assert [[name:'Johnny', age: 36, gender: 'M'],
[name:'Amy', age: 54, gender: 'F']]