Sunday, June 17, 2018

Grails on Groovy > Transform Collection / List to a Map with collectEntries > Get Map From Array List > Convert List To Corresponding Map Entity

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.
List<String> words = ["Grails", "On", "Groovy"]
Map map = words.collectEntries {
    [ (it): it.contains("G") ]
}
println(map)
Which will output as below:
[Grails:true, On:false, Groovy:true]

No comments:

Post a Comment