<p>I'd like some sorthand for this:</p>

<pre><code>Map rowToMap(row) {
    def rowMap = [:];
    row.columns.each{ rowMap[it.name] = it.val }
    return rowMap;
}
</code></pre>

<p>given the way the GDK stuff is, I'd expect to be able to do something like:</p>

<pre><code>Map rowToMap(row) {
    row.columns.collectMap{ [it.name,it.val] }
}
</code></pre>

<p>but I haven't seen anything in the docs... am I missing something? or am I just way too lazy?</p>

[{'answer_id': 18981, 'author': 'danb', 'author_id': 2031, 'author_profile': 'https://Stackoverflow.com/users/2031', 'pm_score': 1, 'selected': False, 'text': '<p>I can\'t find anything built in... but using the ExpandoMetaClass I can do this: </p>\n\n<pre><code>ArrayList.metaClass.collectMap = {Closure callback-&gt;\n    def map = [:]\n    delegate.each {\n        def r = callback.call(it)\n        map[r[0]] = r[1]\n    }\n    return map\n}\n</code></pre>\n\n<p>this adds the collectMap method to all ArrayLists... I\'m not sure why adding it to List or Collection didn\'t work.. I guess that\'s for another question... but now I can do this...</p>\n\n<pre><code>assert ["foo":"oof", "42":"24", "bar":"rab"] ==\n            ["foo", "42", "bar"].collectMap { return [it, it.reverse()] }\n</code></pre>\n\n<p>from List to calculated Map with one closure... exactly what I was looking for. </p>\n\n<p>Edit: the reason I couldn\'t add the method to the interfaces List and Collection was because I did not do this:</p>\n\n<pre><code>List.metaClass.enableGlobally()\n</code></pre>\n\n<p>after that method call, you can add methods to interfaces.. which in this case means my collectMap method will work on ranges like this:</p>\n\n<pre><code>(0..2).collectMap{[it, it*2]}\n</code></pre>\n\n<p>which yields the map: [0:0, 1:2, 2:4]</p>\n'}, {'answer_id': 18996, 'author': 'ogrodnek', 'author_id': 2210, 'author_profile': 'https://Stackoverflow.com/users/2210', 'pm_score': 3, 'selected': False, 'text': '<p>Also, if you\'re use google collections (<a href="http://code.google.com/p/google-collections/" rel="noreferrer">http://code.google.com/p/google-collections/</a>), you can do something like this:</p>\n\n<pre><code>  map = Maps.uniqueIndex(list, Functions.identity());\n</code></pre>\n'}, {'answer_id': 19077, 'author': 'danb', 'author_id': 2031, 'author_profile': 'https://Stackoverflow.com/users/2031', 'pm_score': 3, 'selected': False, 'text': "<p>ok... I've played with this a little more and I think this is a pretty cool method...</p>\n\n<pre><code>def collectMap = {Closure callback-&gt;\n    def map = [:]\n    delegate.each {\n        def r = callback.call(it)\n        map[r[0]] = r[1]\n    }\n    return map\n}\nExpandoMetaClass.enableGlobally()\nCollection.metaClass.collectMap = collectMap\nMap.metaClass.collectMap = collectMap\n</code></pre>\n\n<p>now any subclass of Map or Collection have this method...</p>\n\n<p>here I use it to reverse the key/value in a Map</p>\n\n<pre><code>[1:2, 3:4].collectMap{[it.value, it.key]} == [2:1, 4:3]\n</code></pre>\n\n<p>and here I use it to create a map from a list</p>\n\n<pre><code>[1,2].collectMap{[it,it]} == [1:1, 2:2]\n</code></pre>\n\n<p>now I just pop this into a class that gets called as my app is starting and this method is available throughout my code.</p>\n\n<p>EDIT:</p>\n\n<p>to add the method to all arrays...</p>\n\n<pre><code>Object[].metaClass.collectMap = collectMap\n</code></pre>\n"}, {'answer_id': 149818, 'author': 'Michael Easter', 'author_id': 12704, 'author_profile': 'https://Stackoverflow.com/users/12704', 'pm_score': 0, 'selected': False, 'text': "<p>What about something like this?</p>\n\n<pre><code>// setup\nclass Pair { \n    String k; \n    String v; \n    public Pair(def k, def v) { this.k = k ; this.v = v; }\n}\ndef list = [ new Pair('a', 'b'), new Pair('c', 'd') ]\n\n// the idea\ndef map = [:]\nlist.each{ it -&gt; map.putAt(it.k, it.v) }\n\n// verify\nprintln map['c']\n</code></pre>\n"}, {'answer_id': 198614, 'author': 'Robert Fischer', 'author_id': 27561, 'author_profile': 'https://Stackoverflow.com/users/27561', 'pm_score': 5, 'selected': False, 'text': '<p>Check out "inject".  Real functional programming wonks call it "fold".</p>\n\n<pre><code>columns.inject([:]) { memo, entry -&gt;\n    memo[entry.name] = entry.val\n    return memo\n}\n</code></pre>\n\n<p>And, while you\'re at it, you probably want to define methods as Categories instead of right on the metaClass.  That way, you can define it once for all Collections:</p>\n\n<pre><code>class PropertyMapCategory {\n    static Map mapProperty(Collection c, String keyParam, String valParam) {\n        return c.inject([:]) { memo, entry -&gt;\n            memo[entry[keyParam]] = entry[valParam]\n            return memo\n        }\n    }\n}\n</code></pre>\n\n<p>Example usage: </p>\n\n<pre><code>use(PropertyMapCategory) {\n    println columns.mapProperty(\'name\', \'val\')\n}\n</code></pre>\n'}, {'answer_id': 4484958, 'author': 'Amir Raminfar', 'author_id': 419075, 'author_profile': 'https://Stackoverflow.com/users/419075', 'pm_score': 4, 'selected': False, 'text': '<p>Was the <a href="http://docs.groovy-lang.org/latest/html/api/org/codehaus/groovy/runtime/DefaultGroovyMethods.html#groupBy(java.lang.Iterable,%20groovy.lang.Closure)" rel="noreferrer">groupBy</a> method not available when this question was asked? </p>\n'}, {'answer_id': 5645413, 'author': 'epidemian', 'author_id': 581845, 'author_profile': 'https://Stackoverflow.com/users/581845', 'pm_score': 8, 'selected': True, 'text': '<p>I\'ve recently came across the need to do exactly that: converting a list into a map. This question was posted before Groovy version 1.7.9 came out, so the method <a href="http://docs.groovy-lang.org/latest/html/api/org/codehaus/groovy/runtime/DefaultGroovyMethods.html#collectEntries(java.lang.Iterable,%20groovy.lang.Closure)" rel="noreferrer"><code>collectEntries</code></a> didn\'t exist yet. It works exactly as the <code>collectMap</code> method <a href="https://stackoverflow.com/questions/18538/shortcut-for-creating-a-map-from-a-list-in-groovy/19077#19077">that was proposed</a>:</p>\n\n<pre><code>Map rowToMap(row) {\n    row.columns.collectEntries{[it.name, it.val]}\n}\n</code></pre>\n\n<p>If for some reason you are stuck with an older Groovy version, the <a href="http://docs.groovy-lang.org/latest/html/api/org/codehaus/groovy/runtime/DefaultGroovyMethods.html#inject(java.lang.Object,%20groovy.lang.Closure)" rel="noreferrer"><code>inject</code></a> method can also be used (as proposed <a href="https://stackoverflow.com/questions/18538/shortcut-for-creating-a-map-from-a-list-in-groovy/198614#198614">here</a>). This is a slightly modified version that takes only one expression inside the closure (just for the sake of character saving!):</p>\n\n<pre><code>Map rowToMap(row) {\n    row.columns.inject([:]) {map, col -&gt; map &lt;&lt; [(col.name): col.val]}\n}\n</code></pre>\n\n<p>The <code>+</code> operator can also be used instead of the <code>&lt;&lt;</code>.</p>\n'}, {'answer_id': 42689859, 'author': 'Abbas Gadhia', 'author_id': 638670, 'author_profile': 'https://Stackoverflow.com/users/638670', 'pm_score': 3, 'selected': False, 'text': "<p>If what you need is a simple key-value pair, then the method <code>collectEntries</code> should suffice. For example</p>\n\n<pre><code>def names = ['Foo', 'Bar']\ndef firstAlphabetVsName = names.collectEntries {[it.charAt(0), it]} // [F:Foo, B:Bar]\n</code></pre>\n\n<p>But if you want a structure similar to a Multimap, in which there are multiple values per key, then you'd want to use the <code>groupBy</code> method</p>\n\n<pre><code>def names = ['Foo', 'Bar', 'Fooey']\ndef firstAlphabetVsNames = names.groupBy { it.charAt(0) } // [F:[Foo, Fooey], B:[Bar]]\n</code></pre>\n"}]
['https://Stackoverflow.com/questions/18538', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2031/']