Groovy Goodness: Transform Elements While Flattening [Snippet

published 14.01.2020 15:42

by {{Node.Authors[0].Realname }} from dzone.com

We can use themethod in Groovy to flatten a collection that contains other collections into a single collection with all elements.

We can pass a closure as an extra argument to themethod 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 method without a closure argument.

Then we pass a closure argument and transform the element: