{"id":23034,"date":"2015-07-15T16:28:57","date_gmt":"2015-07-15T10:58:57","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=23034"},"modified":"2016-08-29T14:43:57","modified_gmt":"2016-08-29T09:13:57","slug":"groovy-collectentries-to-get-a-map-from-a-collection","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/groovy-collectentries-to-get-a-map-from-a-collection\/","title":{"rendered":"Groovy collectEntries to get a Map from a Collection"},"content":{"rendered":"<p>Dealing with collections is a part of a <a href=\"http:\/\/www.tothenew.com\/blog\/the-lifestyle-we-adhere-to-to-the-new\/\">Developer\u2019s daily life<\/a>. However, sometimes it becomes quite cumbersome when we have to iterate through each collection every time we want a manipulated Collection.<\/p>\n<p>Ever thought of a <a title=\"Groovy Development\" href=\"http:\/\/www.tothenew.com\/grails-application-development\">groovier way<\/a> to manipulate a collection and get a Map in a single line?<\/p>\n<p>Well, multiple iterations to convert a List to a Map can be saved with collectEntries.<br \/>\nGroovy collectEntries iterates over a collection and return a Map based on the manipulations.<\/p>\n<p>Let us consider a simple class:<\/p>\n<p>[java]<\/p>\n<p>class Person {<br \/>\nInteger salary<br \/>\nString emailId<br \/>\n}<\/p>\n<p>Person person1=new Person(salary:25000,emailId:&quot;anamika@gmail.com&quot;)<br \/>\nPerson person2=new Person(salary:23000,emailId:&quot;sanjana@gmail.com&quot;)<br \/>\nPerson person3=new Person(salary:26000,emailId:&quot;khalid@gmail.com&quot;)<br \/>\nPerson person4=new Person(salary:20000,emailId:&quot;anjali@gmail.com&quot;)<br \/>\nPerson person5=new Person(salary:22000,emailId:&quot;aditya@gmail.com&quot;)<br \/>\nList&lt;Person&gt; persons=[person1,person2,person3,person4,person5]<\/p>\n<p>[\/java]<\/p>\n<p>Given a list of Person Objects,to extract a map containing key as Person\u2019s emailId &amp; value as salary, we can use<\/p>\n<p>[java]<\/p>\n<p>Map&lt;String,Integer&gt; result = persons.collectEntries{<br \/>\n[it.emailId,it.salary]<br \/>\n}<\/p>\n<p>OR<\/p>\n<p>Map&lt;String,Integer&gt; result = persons.collectEntries{<br \/>\n[(it.emailId):it.salary]<br \/>\n}<\/p>\n<p>Output of result -&gt; [anamika@gmail.com:25000, sanjana@gmail.com:23000, khalid@gmail.com:26000, anjali@gmail.com:20000, aditya@gmail.com:22000]<\/p>\n<p>[\/java]<\/p>\n<p>CollectEntries works on any type of Collection-&gt; be it a Map, a list of Objects, Array etc.<\/p>\n<p>Hope it helps \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Dealing with collections is a part of a Developer\u2019s daily life. However, sometimes it becomes quite cumbersome when we have to iterate through each collection every time we want a manipulated Collection. Ever thought of a groovier way to manipulate a collection and get a Map in a single line? Well, multiple iterations to convert [&hellip;]<\/p>\n","protected":false},"author":105,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":1063},"categories":[7,1],"tags":[1988,504,9,1987],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/23034"}],"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\/105"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=23034"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/23034\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=23034"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=23034"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=23034"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}