Search

Dark theme | Light theme

September 12, 2011

Groovy Goodness: Transform Collection to a Map with collectEntries

Since Groovy 1.7.9 we can use the collectEntries method for a Collection to get a Map. We use a closure to transform the elements in the collection to a map entry. And the end result is a Map with all the map entries.

def words = ['Groovy', 'Rocks', 'Big', 'Time']

def result = words.collectEntries { 
    [(it): it.contains('o')]
}

assert result.Groovy && result.Rocks
assert !result.Big && !result.Time