Thursday, 24 January 2019

Groovy: List: flatten: Flatten list elements


Groovy provides ‘flatten’ method, it flattens nested collections in this list.

HelloWorld.groovy
list = [1, [2, 3], [4, [5, [6, 7, [8, 9]]]]]
flattenList = list.flatten()

println "list : $list"
println "flattenList : $flattenList"

Output
list : [1, [2, 3], [4, [5, [6, 7, [8, 9]]]]]
flattenList : [1, 2, 3, 4, 5, 6, 7, 8, 9]


Previous                                                 Next                                                 Home

No comments:

Post a Comment