使用collect闭包改进Groovy列表迭代

时间:2015-12-22 10:42:13

标签: groovy closures

我有一个正常运行的Groovy 2.4.3方法,我认为我可以使用collect()闭包来实现 groovier ,但不完全确定如何:

List<Buzz> deriveBuzzesFromFizz(Fizz fizz) {
    List<Buzz> buzzes = []
    fizz.foobars?.each {
        if(it.label.equals('whistlefeather')) {
            buzzes << it
        }
    }

    buzzes
}

也许是这样的:

List<Buzz> buzz = fizz.foobars?.collect {
    it.label.equals('whistlefeather')
}

......或者附近?!

1 个答案:

答案 0 :(得分:4)

你在想一个findAll

fizz.foobars?.findAll {
    it.label == 'whistlefeather'
}
相关问题