{"id":33,"date":"2009-04-06T18:10:08","date_gmt":"2009-04-06T12:40:08","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=33"},"modified":"2016-12-19T15:28:29","modified_gmt":"2016-12-19T09:58:29","slug":"groovy-collection-method-groupby","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/groovy-collection-method-groupby\/","title":{"rendered":"Groovy Collection method: groupBy()"},"content":{"rendered":"<p><code>public Map groupBy(Closure closure)<\/code><\/p>\n<p>Going through Groovy Collection, I found this powerful method which takes a Closure as parameter &amp; based on the closure, it returns a map in which all unique values returned by closure act as keys.<\/p>\n<p>Lets take an example:<\/p>\n<p>If you want to group integers between 1 to 100 based on the integer value at units place as follows:<\/p>\n<pre lang=\"groovy\">[1:[1, 11, 21, 31, 41, 51, 61, 71, 81, 91],\r\n2:[2, 12, 22, 32, 42, 52, 62, 72, 82, 92],\r\n3:[3, 13, 23, 33, 43, 53, 63, 73, 83, 93],\r\n4:[4, 14, 24, 34, 44, 54, 64, 74, 84, 94],\r\n5:[5, 15, 25, 35, 45, 55, 65, 75, 85, 95],\r\n6:[6, 16, 26, 36, 46, 56, 66, 76, 86, 96],\r\n7:[7, 17, 27, 37, 47, 57, 67, 77, 87, 97],\r\n8:[8, 18, 28, 38, 48, 58, 68, 78, 88, 98],\r\n9:[9, 19, 29, 39, 49, 59, 69, 79, 89, 99],\r\n0:[10, 20, 30, 40, 50, 60, 70, 80, 90, 100]]<\/pre>\n<p>Just think of the code you will need to write in Java for this.<\/p>\n<p>However, its a single method call in Groovy:    <code>(1..100).groupBy{it%10}<\/code><\/p>\n<p>Hope to find more such methods over time.<\/p>\n<p>\u2013<br \/>\n~Aman Aggarwal<br \/>\naman@intelligrape.com<\/p>\n<p><a href=\"http:\/\/www.tothenew.com\/blog\/\">http:\/\/www.IntelliGrape.com\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>public Map groupBy(Closure closure) Going through Groovy Collection, I found this powerful method which takes a Closure as parameter &amp; based on the closure, it returns a map in which all unique values returned by closure act as keys. Lets take an example: If you want to group integers between 1 to 100 based on [&hellip;]<\/p>\n","protected":false},"author":282,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":51},"categories":[7],"tags":[4840,9],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/33"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/282"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=33"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/33\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=33"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=33"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=33"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}