In a previous blog post you can see how to use the collectEntries
method to transform an iterable object into a Map
.
Groovy 5 introduced some extra overloaded methods for collectEntries
.
You can now pass a closure or function as arguments to transform the original iterable element into the key and value for the resulting Map
.
It is now also possible to pass a so-called collector Map
that will be used to extend with new key/value pairs.
Besides extra overloaded method signatures for collectEntries
Groovy 5 also adds the new methods withCollectedKeys
and withCollectedValues
.
With the method withCollectedKeys
a closure or function can passed to create the keys for the new Map
based on the elements from the iterable.
The value of the key/value pair is the unchanged element.
You use the method withCollectedValues
to pass a closure or function to create the value for the new key/value pair in the resulting Map
.
The key will be the original element from the iterable.
You can see in the following example several uses of the new method signatures for collectEntries
and the new methods withCollectedKeys
and withCollectedValues
:
// The first argument of collectEntries method is a function to // create the key and the second argument is a function to // create the value. assert words.collectEntries { it.toLowerCase() } { it.contains('o') } == [groovy: true, rocks: true, big: false, time: false] // You can use a closure or method references. assert words.collectEntries(String::toLowerCase) { it.contains('o') } == [groovy: true, rocks: true, big: false, time: false] // You can pass a collector map that will be used to add the new // map entries to. assert words.collectEntries([apache: false]) { it.toLowerCase() } { it.contains('o') } == [apache: false, groovy: true, rocks: true, big: false, time: false] // If a key is created that already exists in the collector map it // will be overriden. assert words.collectEntries([groovy: false]) { it.toLowerCase() } { it.contains('o') } == [groovy: true, rocks: true, big: false, time: false] // With the collectedWithKeys method you can pass a function // to create the key of the key/value pair. // The original element is the value of the key/value pair. assert words.withCollectedKeys { it.size() } == [6: 'Groovy', 5: 'Rocks', 3: 'Big', 4: 'Time'] assert words.withCollectedKeys(String::size) == [6: 'Groovy', 5: 'Rocks', 3: 'Big', 4: 'Time'] // Here you can pass a collector map as well. assert words.withCollectedKeys([7: 'Awesome']) { it.size() } == [7: 'Awesome', 6: 'Groovy', 5: 'Rocks', 3: 'Big', 4: 'Time'] // An existing key will be overriden. assert words.withCollectedKeys([6: 'Apache']) { it.size() } == [6: 'Groovy', 5: 'Rocks', 3: 'Big', 4: 'Time'] // With the collectedWithValues method you can pass a function // to create the value of the key/value pair. // The original element is the key of the key/value pair. assert words.withCollectedValues { it.size() } == [Groovy: 6, Rocks: 5, Big: 3, Time: 4] assert words.withCollectedValues(String::size) == [Groovy: 6, Rocks: 5, Big: 3, Time: 4] // A collector map can be passed as first argument. assert words.withCollectedValues([Apache: 6], String::size) == [Apache: 6, Groovy: 6, Rocks: 5, Big: 3, Time: 4]
Written with Groovy 5.0.0.