Groovy Method to combine list of lists

Viewed 31640
Input >> list = [[1,2,3], [6], [3,4,5,6]]

Output >> [1,2,3,3,4,5,6,6] 

I want to know if there is something more straightforward than this

l = []
list.each{ l = l + it }
println l

like a default groovy closure or method?

2 Answers

As some people may miss the comment by @Desty, posted as answer here:

Note that the semantics of this are different in that it recurses through the lists, producing different results in situations like this: [[1,2,3], [4,[5]],[6]].flatten(). The cleanest way of doing the OP's concatenation is list.collectMany { it }

Example:

list = [[1,2,3], [6], [3,4,5,6]]
assert list.collectMany{ it }.sort() == [1,2,3,3,4,5,6,6] 
Related