{"id":7368,"date":"2012-09-12T14:49:31","date_gmt":"2012-09-12T09:19:31","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=7368"},"modified":"2014-12-17T09:14:19","modified_gmt":"2014-12-17T03:44:19","slug":"two-interesting-methods-in-groovy-2-0-takewhile-and-dropwhile","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/two-interesting-methods-in-groovy-2-0-takewhile-and-dropwhile\/","title":{"rendered":"Two Interesting methods in Groovy 2.0 : takeWhile and dropWhile"},"content":{"rendered":"<p>Today I found two very interesting methods: takeWhile and dropWhile. The names itself reveals their work i.e. takeWhile (consider while some condition is true) and dropWhile (ignore while some condition is true).<br \/>\n<br \/>\n Although, these methods have been added to Groovy 1.8.7, but for character sequence, these methods are supported in Groovy 2.0 only.<br \/>\n<br \/>\nThe <strong>takeWhile<\/strong> method considers each element one by one in order of definition till the condition does not fail.<br \/>\ne.g.<\/p>\n<p>[java]<br \/>\nList marks = [2,1,4,8,5,7,9,12]<\/p>\n<p>println marks.takeWhile{it &lt; 6} <\/p>\n<p>\/\/Output: [2, 1, 4]<br \/>\n[\/java]<br \/>\nAs soon as <strong>takeWhile<\/strong> method found an element greater than 6 (in above example) by processing elements in the order of definition, it stopped considering more elements.<br \/>\n<br \/>\nUsing <strong>takeWhile<\/strong> method with Map:<\/p>\n<p>[java]<br \/>\nMap m  = [&quot;groovy&quot;:8,&quot;grails&quot;:9,&quot;database&quot;:6,&quot;java&quot;:1,&quot;gorm&quot;:7,&quot;html&quot;:2,&quot;javascript&quot;:3]<\/p>\n<p>println m.takeWhile{it.value &gt; 7 } <\/p>\n<p>\/\/Output:  [groovy:8, grails:9]<\/p>\n<p>println m.takeWhile{it.key != &quot;html&quot; } <\/p>\n<p>\/\/Output: [groovy:8, grails:9, database:6, java:1, gorm:7]<\/p>\n<p>[\/java]<\/p>\n<p>Using <strong>takeWhile<\/strong> method with char sequence: <\/p>\n<p>[java]<br \/>\nString line = &quot;Grails is a web framework based on the Groovy language.&quot;<\/p>\n<p>println line.takeWhile { it &lt; &#8216;x&#8217; }  <\/p>\n<p>\/\/Output: Grails is a web framework based on the Groov<\/p>\n<p>[\/java]<\/p>\n<p>The <strong>takeWhile<\/strong> method considers each character in character sequence one by one till the condition does not fail.<br \/>\n<br \/>\nMoving on to <strong>dropWhile<\/strong> method, It ignores all the elements in order of definition till the condition does not meet.<\/p>\n<p>[java]<br \/>\nList marks = [&#8216;c&#8217;,&#8217;g&#8217;,&#8217;f&#8217;,&#8217;n&#8217;,&#8217;b&#8217;,&#8217;h&#8217;,&#8217;y&#8217;]<\/p>\n<p>println marks.dropWhile{it &lt; &#8216;m&#8217;} \/\/ It will print [n, b, h, y]<br \/>\n[\/java]<\/p>\n<p>In above example, the <strong>dropWhile<\/strong> method ignored all the list elements(&#8216;c&#8217;,&#8217;g&#8217;,&#8217;f&#8217;,&#8217;n&#8217;) till the condition did not meet.<\/p>\n<p>Using <strong>dropWhile<\/strong> method with map<\/p>\n<p>[java]<br \/>\nMap m  = [&quot;groovy&quot;:4,&quot;grails&quot;:6,&quot;database&quot;:9,&quot;java&quot;:1,&quot;gorm&quot;:7,&quot;html&quot;:2,&quot;javascript&quot;:3]<\/p>\n<p>println m.dropWhile{it.value &lt; 7 } <\/p>\n<p>\/\/ Output: [database:9, java:1, gorm:7, html:2, javascript:3]<\/p>\n<p>println m.dropWhile{it.key != &quot;html&quot; } <\/p>\n<p>\/\/ Output: [html:2, javascript:3]<br \/>\n[\/java]<\/p>\n<p>Using <strong>takeWhile<\/strong> method with char sequence<\/p>\n<p>[java]<br \/>\nString line = &quot;Grails is a web framework based on the Groovy language.&quot;<\/p>\n<p>println line.dropWhile { it &lt; &#8216;x&#8217; }  \/\/y language.<\/p>\n<p>[\/java]<\/p>\n<p><a href=\"http:\/\/www.tothenew.com\/blog\/author\/divya\/\">Divya Setia<\/a><br \/>\n<a href=\"mailto:divya@intelligrape.com\">divya@intelligrape.com<\/a><br \/>\n<a href=\"https:\/\/twitter.com\/divs157\">https:\/\/twitter.com\/divs157<\/a> <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today I found two very interesting methods: takeWhile and dropWhile. The names itself reveals their work i.e. takeWhile (consider while some condition is true) and dropWhile (ignore while some condition is true). Although, these methods have been added to Groovy 1.8.7, but for character sequence, these methods are supported in Groovy 2.0 only. The takeWhile [&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":30},"categories":[7],"tags":[958,9,775,957],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/7368"}],"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=7368"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/7368\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=7368"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=7368"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=7368"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}