Split collection into sub collections in Groovy

前端 未结 3 1850
轮回少年
轮回少年 2021-01-14 05:34

I have an array containing an unknown number of items that I would like to split into separate arrays so that each separate array contains no more than 4 items. What is the

3条回答
  •  我在风中等你
    2021-01-14 06:24

    Since Groovy 1.8.6, you can use collate:

    def letters = 'a'..'g'
    assert letters.collate(3) == [['a', 'b', 'c'], ['d', 'e', 'f'], ['g']]
    
    def letters = 'a'..'g'
    assert letters.collate(3) == [['a', 'b', 'c'], ['d', 'e', 'f'], ['g']]
    

    Credit to Mrhaki's Groovy goodness series: http://mrhaki.blogspot.com.au/2012/04/groovy-goodness-collate-list-into-sub.html

提交回复
热议问题