简体   繁体   English

将集合分成块并在 Groovy 中对每个块调用闭包

[英]Divide a collection into bulks and invoke a closure on each bulk in Groovy

I wonder there might be a built-in feature in Groovy, something like我想知道 Groovy 中可能有一个内置功能,比如

aCollection.doEachBulk(100) { bulk -> 
    ...
}

Is there?在那儿?

I'm not sure if I understand your question correctly, but you may be looking for the collate method.我不确定我是否正确理解你的问题,但你可能正在寻找collate方法。

(1..20).collate(5).each { subRange ->
    println subRange
}

Output: Output:

[1, 2, 3, 4, 5]
[6, 7, 8, 9, 10]
[11, 12, 13, 14, 15]
[16, 17, 18, 19, 20]

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

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