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?
Combine lists using the plus operator The plus operator will return a new list containing all the elements of the two lists while and the addAll method appends the elements of the second list to the end of the first one. Obviously, the output is the same as the one using addAll method.
Groovy - add() Append the new value to the end of this List. This method has 2 different variants. boolean add(Object value) − Append the new value to the end of this List.
Try flatten
, ie:
list.flatten()
Or, to get the output you want:
list = [[1,2,3], [6], [3,4,5,6]] assert list.flatten().sort() == [1,2,3,3,4,5,6,6]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With