list 组合列表列表的 Groovy 方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11558450/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 01:52:05  来源:igfitidea点击:

Groovy Method to combine list of lists

listgroovy

提问by Vamsi Emani

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?

像默认的 groovy 闭包或方法?

回答by tim_yates

Try flatten, ie:

尝试flatten,即:

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]