{"id":8748,"date":"2012-09-26T22:49:27","date_gmt":"2012-09-26T17:19:27","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=8748"},"modified":"2012-09-26T22:52:23","modified_gmt":"2012-09-26T17:22:23","slug":"using-inject-method-in-groovy-2-0","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/using-inject-method-in-groovy-2-0\/","title":{"rendered":"Using Inject method in Groovy 2.0"},"content":{"rendered":"<p>A very useful enhanced method in <strong>Groovy 2.0<\/strong> is <strong>inject<\/strong> method whose key purpose is to pass the outcome of first iteration to next iteration and keep on passing till all the elements of collections are processed.<br \/>\n<br \/>\nLets start with an example, consider a class <strong>Employee<\/strong> with name and salary as attributes:<\/p>\n<p>[java]<br \/>\nclass Employee{<br \/>\n     String name<br \/>\n     Float salary<br \/>\n}<\/p>\n<p>Employee emp1 = new Employee(name:&quot;Divya&quot;,salary:50000)<br \/>\nEmployee emp2 = new Employee(name:&quot;Priya&quot;,salary:30000)<br \/>\nEmployee emp3 = new Employee(name:&quot;Ginny&quot;,salary:60000)<br \/>\nEmployee emp4 = new Employee(name:&quot;Shreya&quot;,salary:80000)<br \/>\nEmployee emp5 = new Employee(name:&quot;Heena&quot;,salary:10000)<\/p>\n<p>List&lt;Employee&gt; employees = [emp1,emp2,emp3,emp4,emp5]<br \/>\n[\/java]<br \/>\n<\/p>\n<p> And if we have to find out sum of salaries of those employees whose salary is greater than 40000, then most cleaner way of doing the same is using the <strong>inject<\/strong> method which also prevent us to define a new variable for doing the same.<\/p>\n<p>[java]<br \/>\nFloat totalSalaryOfUsers = employees*.salary.inject{accumulator,currentValue-&gt; accumulator  + (currentValue &gt; 40000 ? currentValue : 0) }<\/p>\n<p>println totalSalaryOfUsers<\/p>\n<p>\/\/ Output : 190000.0<br \/>\n[\/java]<\/p>\n<p>In previous version of Groovy(&lt;2.0), there was need to initialize the inject method with a value, but in Groovy 2.0, inject method initializes itself with the first element of collection.<br \/>\n<br \/>\nConsider one more example of inject method for clear understanding, i.e. ,converting list to String is:<\/p>\n<p>[java]<br \/>\nList&lt;String&gt; thought = [&quot;Don&#8217;t&quot;,&quot;put&quot;,&quot;off&quot;,&quot;till&quot;,&quot;tomorrow&quot;,&quot;what&quot;,&quot;you&quot;,&quot;can&quot;,&quot;do&quot;,&quot;today&quot;]<\/p>\n<p>String strThought = thought.inject{accumulator,currentValue-&gt; accumulator + &quot; &quot; + currentValue }<\/p>\n<p>println strThought<\/p>\n<p>\/\/ Output: Don&#8217;t put off till tomorrow what you can do today<\/p>\n<p>[\/java]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A very useful enhanced method in Groovy 2.0 is inject method whose key purpose is to pass the outcome of first iteration to next iteration and keep on passing till all the elements of collections are processed. Lets start with an example, consider a class Employee with name and salary as attributes: [java] class Employee{ [&hellip;]<\/p>\n","protected":false},"author":29,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":6},"categories":[1],"tags":[9,1086,1084,1085],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/8748"}],"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\/29"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=8748"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/8748\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=8748"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=8748"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=8748"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}