In Groovy we can use the collectMany()
method to transform items from a collection into a collection. The resulting collection is then flattened into a single collection. This means we can use the closure to return a collection with values and at the end all these collections are flattened into a single collection.
def text = 'groovy' as String[] def mixedCase = text.collectMany { [it, it.toUpperCase()] } assert mixedCase == ['g','G','r','R','o','O','o','O','v','V','y','Y'] assert text == 'groovy' as String[] def list = [12, 20, 34] def result = list.collectMany { [it, it*2, it*3] } assert result == [12,24,36,20,40,60,34,68,102] assert list == [12, 20, 34]