简体   繁体   English

通过collect封闭改进Groovy列表迭代

[英]Improving Groovy list iteration with collect closure

I have a functioning Groovy 2.4.3 method that I think I can make even groovier by using the collect() closure, but not exactly sure how: 我有一个可以正常运行的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
}

Perhaps something like: 也许像这样:

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

...or thereabouts?! ...或附近?

You're thinking of a findAll 您正在考虑findAll

fizz.foobars?.findAll {
    it.label == 'whistlefeather'
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM