{"id":8920,"date":"2012-09-26T23:58:05","date_gmt":"2012-09-26T18:28:05","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=8920"},"modified":"2012-09-27T00:15:26","modified_gmt":"2012-09-26T18:45:26","slug":"findresults-and-findresult-methods-of-groovy","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/findresults-and-findresult-methods-of-groovy\/","title":{"rendered":"FindResults and FindResult Methods of Groovy"},"content":{"rendered":"<p>In almost all the applications that we work on, we have to transform elements of a collection in one way or the other. We can do it in different ways.<\/p>\n<p>Let there be a domain &#8220;Employee&#8221; with following attributes:<\/p>\n<p style=\"padding-bottom:5px\">\n<p>[java]<br \/>\nclass Employee{<br \/>\nString firstName<br \/>\nString lastName<br \/>\nDouble salary<br \/>\n}<br \/>\n[\/java]<\/p>\n<p style=\"padding-bottom:5px\">\n<p>And we have 5 Employees:<br \/>\n[java]<br \/>\nEmployee employee1 = new Employee(firstName:&quot;John&quot;, lastName:&quot;Doe&quot;, salary:10000)<br \/>\nEmployee employee2 = new Employee(firstName:&quot;Tim&quot;, lastName:&quot;Kerry&quot;, salary:20000)<br \/>\nEmployee employee3 = new Employee(firstName:&quot;Kim&quot;, lastName:&quot;Terry&quot;, salary:30000)<br \/>\nEmployee employee4 = new Employee(firstName:&quot;Mary&quot;, lastName:&quot;Jin&quot;, salary:40000)<br \/>\nEmployee employee5 = new Employee(firstName:&quot;James&quot;, lastName:&quot;Sally&quot;, salary:50000)<br \/>\n[\/java]<\/p>\n<p style=\"padding-bottom:5px\">\n<p>If we want full name of all the employees having salary greater than 25000.<br \/>\nIf we use collect in following way:<br \/>\n[java]<br \/>\nprintln Employee.list().collect{it.salary&gt;25000?(firstName+&#8217; &#8216;+lastName):null}<br \/>\n\/\/The gives [null,null,Kim Terry,Mary Jin,James Sally]<br \/>\n[\/java]<\/p>\n<p style=\"padding-bottom:5px\">\n<p>The problem with collect is that the size of transformed list is equal to the original list even if the transformed element is null. So we need to have an additional<br \/>\nstep to get non-null elements from the transformed list.<\/p>\n<p style=\"padding-bottom:5px\">\n<p>Instead of this, we can simply use findResults in following way:<br \/>\n[java]<br \/>\nprintln Employee.list().findResults{it.salary&gt;25000?(firstName+&#8217; &#8216;+lastName):null}<br \/>\n\/\/This gives a list of size equal to total non-null elements(3 in this case) : [Kim Terry,Mary Jin,James Sally].<br \/>\n[\/java]<\/p>\n<p style=\"padding-bottom:5px\">\n<p>Further if we want to get just the first non-null element after transformation, we can use findResult instead of findResults.<br \/>\n[java]<br \/>\nprintln Employee.list().findResult{it.salary&gt;25000?(firstName+&#8217; &#8216;+lastName):null}<br \/>\n\/\/This gives &quot;Kim Terry&quot;.<br \/>\n[\/java]<\/p>\n<p style=\"padding-bottom:5px\">\n<p style=\"padding-bottom:5px\">\n<p>I found it very useful.<\/p>\n<p>Hope it heps you too,:)<\/p>\n<p style=\"padding-bottom:5px\">\n<p>Vivek Sachdeva<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In almost all the applications that we work on, we have to transform elements of a collection in one way or the other. We can do it in different ways. Let there be a domain &#8220;Employee&#8221; with following attributes: [java] class Employee{ String firstName String lastName Double salary } [\/java] And we have 5 Employees: [&hellip;]<\/p>\n","protected":false},"author":48,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":91},"categories":[7],"tags":[1090,1091,9],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/8920"}],"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\/48"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=8920"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/8920\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=8920"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=8920"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=8920"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}