简体   繁体   中英

Groovy each method/closure

What is the difference between using

teams.each(){ team->
//iterate through each team
}

and

teams.each { team->
//iterate through each team
}

Please note in the first one there is a bracket after each and before the closure. We were doing some load testing on our application and noticed that when we use the first one, there are some threads waiting on at this call. But in the second case all works smoothly.

I am curious to know what is the difference here.

Nothing, they are the same thing. In Groovy, if the last parameter to a function is a Closure, then it can occur outside the braces, so for example:

[1,2,3].inject( 0 ) { acc, it -> acc + it }

and

[1,2,3].inject( 0, { acc, it-> acc + it } )

Are the same thing. The usual way of writing groovy is to miss out the braces in your example, or put the closure outside the braces in the inject example above

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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