{"id":33,"date":"2009-04-06T18:10:08","date_gmt":"2009-04-06T12:40:08","guid":{"rendered":"https:\/\/tothenewco.pro\/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:\/\/tothenewco.pro\/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=\"https:\/\/tothenewco.pro\/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":81,"footnotes":""},"categories":[7],"tags":[4840,9],"class_list":["post-33","post","type-post","status-publish","format-standard","hentry","category-grails","tag-grails","tag-groovy"],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 5.0.0.1 - aioseo.com -->\n\t<meta name=\"description\" content=\"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\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"Aman Aggarwal\"\/>\n\t<link rel=\"canonical\" href=\"https:\/\/tothenewco.pro\/blog\/groovy-collection-method-groupby\/\" \/>\n\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 5.0.0.1\" \/>\n\t\t<meta property=\"og:locale\" content=\"en_US\" \/>\n\t\t<meta property=\"og:site_name\" content=\"TO THE NEW BLOG\" \/>\n\t\t<meta property=\"og:type\" content=\"blog\" \/>\n\t\t<meta property=\"og:title\" content=\"Groovy Collection method: groupBy() | TO THE NEW Blog\" \/>\n\t\t<meta property=\"og:description\" content=\"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\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/tothenewco.pro\/blog\/groovy-collection-method-groupby\/\" \/>\n\t\t<meta property=\"og:image\" content=\"https:\/\/tothenewco.pro\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png\" \/>\n\t\t<meta property=\"og:image:secure_url\" content=\"https:\/\/tothenewco.pro\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png\" \/>\n\t\t<meta name=\"twitter:card\" content=\"summary\" \/>\n\t\t<meta name=\"twitter:site\" content=\"@tothenew\" \/>\n\t\t<meta name=\"twitter:title\" content=\"Groovy Collection method: groupBy() | TO THE NEW Blog\" \/>\n\t\t<meta name=\"twitter:description\" content=\"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\" \/>\n\t\t<meta name=\"twitter:image\" content=\"https:\/\/tothenewco.pro\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png\" \/>\n\t\t<script type=\"application\/ld+json\" class=\"aioseo-schema\">\n\t\t\t{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/groovy-collection-method-groupby\\\/#article\",\"name\":\"Groovy Collection method: groupBy() | TO THE NEW Blog\",\"headline\":\"Groovy Collection method: groupBy()\",\"author\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/aman-2\\\/#author\"},\"publisher\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#organization\"},\"datePublished\":\"2009-04-06T18:10:08+05:30\",\"dateModified\":\"2016-12-19T15:28:29+05:30\",\"inLanguage\":\"en-US\",\"commentCount\":7,\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/groovy-collection-method-groupby\\\/#webpage\"},\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/groovy-collection-method-groupby\\\/#webpage\"},\"articleSection\":\"Grails, Grails, Groovy\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/groovy-collection-method-groupby\\\/#breadcrumblist\",\"itemListElement\":[{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog#listItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.tothenew.com\\\/blog\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/grails\\\/#listItem\",\"name\":\"Grails\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/grails\\\/#listItem\",\"position\":2,\"name\":\"Grails\",\"item\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/grails\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/groovy-collection-method-groupby\\\/#listItem\",\"name\":\"Groovy Collection method: groupBy()\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog#listItem\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/groovy-collection-method-groupby\\\/#listItem\",\"position\":3,\"name\":\"Groovy Collection method: groupBy()\",\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/grails\\\/#listItem\",\"name\":\"Grails\"}}]},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#organization\",\"name\":\"TO THE NEW Blog\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/aman-2\\\/#author\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/aman-2\\\/\",\"name\":\"Aman Aggarwal\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/groovy-collection-method-groupby\\\/#authorImage\",\"url\":\"https:\\\/\\\/newersworld-sf-static.tothenew.net\\\/prod\\\/profilePicFolder\\\/66589b6a-51bb-47a9-98a1-5921aac69bd9_aman.aggarwal@tothenew.com.jpg\",\"width\":96,\"height\":96,\"caption\":\"Aman Aggarwal\"}},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/groovy-collection-method-groupby\\\/#webpage\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/groovy-collection-method-groupby\\\/\",\"name\":\"Groovy Collection method: groupBy() | TO THE NEW Blog\",\"description\":\"public Map groupBy(Closure closure) Going through Groovy Collection, I found this powerful method which takes a Closure as parameter & 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\",\"inLanguage\":\"en-US\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#website\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/groovy-collection-method-groupby\\\/#breadcrumblist\"},\"author\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/aman-2\\\/#author\"},\"creator\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/aman-2\\\/#author\"},\"datePublished\":\"2009-04-06T18:10:08+05:30\",\"dateModified\":\"2016-12-19T15:28:29+05:30\"},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/\",\"name\":\"TO THE NEW Blog\",\"inLanguage\":\"en-US\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#organization\"}}]}\n\t\t<\/script>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"Groovy Collection method: groupBy() | TO THE NEW Blog","description":"public Map groupBy(Closure closure) Going through Groovy Collection, I found this powerful method which takes a Closure as parameter & 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","canonical_url":"https:\/\/tothenewco.pro\/blog\/groovy-collection-method-groupby\/","robots":"max-image-preview:large","keywords":"","webmasterTools":{"miscellaneous":""},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/tothenewco.pro\/blog\/groovy-collection-method-groupby\/#article","name":"Groovy Collection method: groupBy() | TO THE NEW Blog","headline":"Groovy Collection method: groupBy()","author":{"@id":"https:\/\/tothenewco.pro\/blog\/author\/aman-2\/#author"},"publisher":{"@id":"https:\/\/tothenewco.pro\/blog\/#organization"},"datePublished":"2009-04-06T18:10:08+05:30","dateModified":"2016-12-19T15:28:29+05:30","inLanguage":"en-US","commentCount":7,"mainEntityOfPage":{"@id":"https:\/\/tothenewco.pro\/blog\/groovy-collection-method-groupby\/#webpage"},"isPartOf":{"@id":"https:\/\/tothenewco.pro\/blog\/groovy-collection-method-groupby\/#webpage"},"articleSection":"Grails, Grails, Groovy"},{"@type":"BreadcrumbList","@id":"https:\/\/tothenewco.pro\/blog\/groovy-collection-method-groupby\/#breadcrumblist","itemListElement":[{"@type":"ListItem","@id":"https:\/\/tothenewco.pro\/blog#listItem","position":1,"name":"Home","item":"https:\/\/tothenewco.pro\/blog","nextItem":{"@type":"ListItem","@id":"https:\/\/tothenewco.pro\/blog\/category\/grails\/#listItem","name":"Grails"}},{"@type":"ListItem","@id":"https:\/\/tothenewco.pro\/blog\/category\/grails\/#listItem","position":2,"name":"Grails","item":"https:\/\/tothenewco.pro\/blog\/category\/grails\/","nextItem":{"@type":"ListItem","@id":"https:\/\/tothenewco.pro\/blog\/groovy-collection-method-groupby\/#listItem","name":"Groovy Collection method: groupBy()"},"previousItem":{"@type":"ListItem","@id":"https:\/\/tothenewco.pro\/blog#listItem","name":"Home"}},{"@type":"ListItem","@id":"https:\/\/tothenewco.pro\/blog\/groovy-collection-method-groupby\/#listItem","position":3,"name":"Groovy Collection method: groupBy()","previousItem":{"@type":"ListItem","@id":"https:\/\/tothenewco.pro\/blog\/category\/grails\/#listItem","name":"Grails"}}]},{"@type":"Organization","@id":"https:\/\/tothenewco.pro\/blog\/#organization","name":"TO THE NEW Blog","url":"https:\/\/tothenewco.pro\/blog\/"},{"@type":"Person","@id":"https:\/\/tothenewco.pro\/blog\/author\/aman-2\/#author","url":"https:\/\/tothenewco.pro\/blog\/author\/aman-2\/","name":"Aman Aggarwal","image":{"@type":"ImageObject","@id":"https:\/\/tothenewco.pro\/blog\/groovy-collection-method-groupby\/#authorImage","url":"https:\/\/newersworld-sf-static.tothenew.net\/prod\/profilePicFolder\/66589b6a-51bb-47a9-98a1-5921aac69bd9_aman.aggarwal@tothenew.com.jpg","width":96,"height":96,"caption":"Aman Aggarwal"}},{"@type":"WebPage","@id":"https:\/\/tothenewco.pro\/blog\/groovy-collection-method-groupby\/#webpage","url":"https:\/\/tothenewco.pro\/blog\/groovy-collection-method-groupby\/","name":"Groovy Collection method: groupBy() | TO THE NEW Blog","description":"public Map groupBy(Closure closure) Going through Groovy Collection, I found this powerful method which takes a Closure as parameter & 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","inLanguage":"en-US","isPartOf":{"@id":"https:\/\/tothenewco.pro\/blog\/#website"},"breadcrumb":{"@id":"https:\/\/tothenewco.pro\/blog\/groovy-collection-method-groupby\/#breadcrumblist"},"author":{"@id":"https:\/\/tothenewco.pro\/blog\/author\/aman-2\/#author"},"creator":{"@id":"https:\/\/tothenewco.pro\/blog\/author\/aman-2\/#author"},"datePublished":"2009-04-06T18:10:08+05:30","dateModified":"2016-12-19T15:28:29+05:30"},{"@type":"WebSite","@id":"https:\/\/tothenewco.pro\/blog\/#website","url":"https:\/\/tothenewco.pro\/blog\/","name":"TO THE NEW Blog","inLanguage":"en-US","publisher":{"@id":"https:\/\/tothenewco.pro\/blog\/#organization"}}]},"og:locale":"en_US","og:site_name":"TO THE NEW BLOG","og:type":"blog","og:title":"Groovy Collection method: groupBy() | TO THE NEW Blog","og:description":"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","og:url":"https:\/\/tothenewco.pro\/blog\/groovy-collection-method-groupby\/","og:image":"https:\/\/tothenewco.pro\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png","og:image:secure_url":"https:\/\/tothenewco.pro\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png","twitter:card":"summary","twitter:site":"@tothenew","twitter:title":"Groovy Collection method: groupBy() | TO THE NEW Blog","twitter:description":"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","twitter:image":"https:\/\/tothenewco.pro\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png"},"aioseo_meta_data":{"post_id":"33","title":null,"description":null,"keywords":null,"keyphrases":null,"primary_term":null,"canonical_url":null,"og_title":null,"og_description":null,"og_object_type":"default","og_image_type":"default","og_image_url":null,"og_image_width":null,"og_image_height":null,"og_image_custom_url":null,"og_image_custom_fields":null,"og_video":null,"og_custom_url":null,"og_article_section":null,"og_article_tags":null,"twitter_use_og":false,"twitter_card":"default","twitter_image_type":"default","twitter_image_url":null,"twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_title":null,"twitter_description":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":[],"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"Article","isEnabled":true},"graphs":[]},"schema_type":null,"schema_type_options":null,"pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":null,"robots_max_videopreview":null,"robots_max_imagepreview":"large","priority":null,"frequency":null,"local_seo":null,"limit_modified_date":false,"created":"2021-04-29 13:57:13","updated":"2024-02-29 09:00:12","focus_keyword":null,"additional_keywords":null,"truseo_locale":null,"ai":null,"breadcrumb_settings":null,"seo_analyzer_scan_date":null},"aioseo_breadcrumb":"<div class=\"aioseo-breadcrumbs\"><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/tothenewco.pro\/blog\" title=\"Home\">Home<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/tothenewco.pro\/blog\/category\/grails\/\" title=\"Grails\">Grails<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\tGroovy Collection method: groupBy()\n\t\t<\/span><\/div>","aioseo_breadcrumb_json":[{"label":"Home","link":"https:\/\/tothenewco.pro\/blog"},{"label":"Grails","link":"https:\/\/tothenewco.pro\/blog\/category\/grails\/"},{"label":"Groovy Collection method: groupBy()","link":"https:\/\/tothenewco.pro\/blog\/groovy-collection-method-groupby\/"}],"_links":{"self":[{"href":"https:\/\/tothenewco.pro\/blog\/wp-json\/wp\/v2\/posts\/33","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tothenewco.pro\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tothenewco.pro\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tothenewco.pro\/blog\/wp-json\/wp\/v2\/users\/282"}],"replies":[{"embeddable":true,"href":"https:\/\/tothenewco.pro\/blog\/wp-json\/wp\/v2\/comments?post=33"}],"version-history":[{"count":0,"href":"https:\/\/tothenewco.pro\/blog\/wp-json\/wp\/v2\/posts\/33\/revisions"}],"wp:attachment":[{"href":"https:\/\/tothenewco.pro\/blog\/wp-json\/wp\/v2\/media?parent=33"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tothenewco.pro\/blog\/wp-json\/wp\/v2\/categories?post=33"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tothenewco.pro\/blog\/wp-json\/wp\/v2\/tags?post=33"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}