这里仍然是新手,但我试图从我的地图列表中的每个地图中获取所有值,其中键的数量等于“橙子”
def resultSet = [
["Oranges":123456, "Apples": "none"],["Oranges":7890, "Apples": "some"]
]
def fruit = resultSet.each{
it.findAll{key, value -> key == "Oranges"}.value
}
println fruit
所以为此,我希望结果为:[123456, 7890]
但我得到了:[[Oranges:123456, Apples:none], [Oranges:7890, Apples:some]]
答案 0 :(得分:5)
each
方法的返回是集合本身。您希望对集合应用某种过滤器,而不是对其每个项目应用。我建议使用findResults
,因为它会过滤掉空值和空值:
def resultSet = [
["Oranges":123456, "Apples": "none"],
["Oranges":7890, "Apples": "some"]
]
def fruit = resultSet.findResults { it.Oranges?.value }
assert fruit == [123456, 7890]