The documentation for Collection.groupBy is a little ambiguous in that it does not make it absolutely clear that the return value is a Map of keys pointing to values that are ArrayLists.
A new method, Collection.collate(Closure) would be a nice addition, to allow collation of a collection of objects into a map keyed on some value provided by a closure, without returning a List for every value. This might be implemented so:
java.util.Map collate(Closure collator) {
def result = [:]
this.each() {
result[collator.call(it)] = it
}
}
Description
The documentation for Collection.groupBy is a little ambiguous in that it does not make it absolutely clear that the return value is a Map of keys pointing to values that are ArrayLists.
A new method, Collection.collate(Closure) would be a nice addition, to allow collation of a collection of objects into a map keyed on some value provided by a closure, without returning a List for every value. This might be implemented so:
java.util.Map collate(Closure collator) {
def result = [:]
this.each() {
result[collator.call(it)] = it
}
}
Paul King added a comment - 23/Dec/07 12:41 AM - edited
There was already a groupBy for collections and one for maps. Rather than introduce a collate() method I rejigged the existing groupBy on Maps. This seemed to be the most consistent thing to do. I hope this yields what you wanted. The doco has also been improved.
Paul King added a comment - 23/Dec/07 12:41 AM - edited There was already a groupBy for collections and one for maps. Rather than introduce a collate() method I rejigged the existing groupBy on Maps. This seemed to be the most consistent thing to do. I hope this yields what you wanted. The doco has also been improved.
The end result is the following:
The end result is the following: