Groovy Goodness: Transform Elements While Flattening
We can use the flatten
method in Groovy to flatten a collection that contains other collections into a single collection with all elements. We can pass a closure as extra argument to the flatten
method to transform each element that is flattened. The argument of the closure is the element from the original collection.
In the following example we first use the flatten
method without a closure argument. Then we pass a closure argument and transform the element: