此处对于groovy还是一个新手,但是我试图从键列表中等于“Oranges”的地图列表中的每个地图中获取所有值

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]]

最佳答案

each方法的返回是集合本身。您想对集合应用某种过滤器,而不是对每个项目应用。我建议使用findResults,因为它会过滤掉null和空值:

def resultSet = [
    ["Oranges":123456, "Apples": "none"],
    ["Oranges":7890, "Apples": "some"]
]

def fruit = resultSet.findResults { it.Oranges?.value }

assert fruit == [123456, 7890]

10-02 11:01