{"id":7621,"date":"2012-09-17T20:29:13","date_gmt":"2012-09-17T14:59:13","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=7621"},"modified":"2014-12-17T09:24:04","modified_gmt":"2014-12-17T03:54:04","slug":"understanding-groovy-withlazydefault-and-witheagerdefault-methods","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/understanding-groovy-withlazydefault-and-witheagerdefault-methods\/","title":{"rendered":"Understanding Groovy 2.0 withLazyDefault and withEagerDefault methods"},"content":{"rendered":"<p>Sometimes there is need to get some default value in place of null values from a list, that was the reason I came across: <strong>withLazyDefault<\/strong> and <strong>withEagerDefault<\/strong> methods.<br \/>\n<br \/>\nMy use case consisted of a list of user names with possibility of null elements, hence wherever the element was null, I wanted to return \u201cunknown\u201d. Although there was other ways of doing the same, but most cleaner way was using <strong>withLazyDefault<\/strong>.<br \/>\n<br \/>\nThese methods are also useful when a list is accessed for a index which doesnot exist i.e. avoiding index out of bound exception. And for map, these methods are useful when map is accessed for a key, whose entry(key-value pair) does not exist.<br \/>\n<br \/>\nLets start with <strong>withLazyDefault<\/strong> method. We can also use <strong>withDefault<\/strong> instead of <strong>withLazyDefault<\/strong> because <strong>withDefault<\/strong> method calls <strong>withLazyDefault<\/strong> internally.<\/p>\n<p>[java]<br \/>\nList&lt;String&gt; languages = [&quot;C&quot;,&quot;C++&quot;,null, &quot;Flex&quot;,&quot;VB&quot;,&quot;Java&quot;].withDefault{&quot;Groovy&quot;}<\/p>\n<p>println languages.get(2)<\/p>\n<p>\/\/Output : Groovy,  (even though at index 2, value is null)<br \/>\n[\/java]<\/p>\n<p>As the value at index 2 was null, but because of default value set at initialization time, langauges.get(2) returned &#8220;Groovy&#8221; as value.<\/p>\n<p>Now, If a list is accessed for a index (lets say m<sup>th<\/sup> index) greater than languages.size()-1 (lets say n<sup>th<\/sup> index, list&#8217;s actual size), and if default value is set using <strong>withDefault<\/strong>(withLazyDefault), then list grows to the size (m+1). The gap in between is filled with null values, although access to list using any index in gap will return default value.<\/p>\n<p>e.g.<\/p>\n<p>[java]<br \/>\n List&lt;String&gt; languages = [&quot;C&quot;,&quot;C++&quot;,null, &quot;Flex&quot;,&quot;VB&quot;,&quot;Java&quot;].withDefault{&quot;Groovy&quot;}<\/p>\n<p>println languages.size()<br \/>\n\/\/Output : 6<\/p>\n<p>println languages.get(10)<br \/>\n\/\/Output : Groovy<\/p>\n<p>println languages.size()<br \/>\n\/\/Output : 11<\/p>\n<p>println languages<br \/>\n\/\/ [C, C++, null, Flex, VB, Java, null, null, null, null, Groovy]<\/p>\n<p>println languages.get(8)<br \/>\n\/\/ Output : Groovy<\/p>\n<p>[\/java]<\/p>\n<p>As you will see, no indexOutOfBoundException will occur and size will grow to the specified index.<\/p>\n<p>Usage with Map:<\/p>\n<p>[java]<br \/>\nMap&lt;String,Integer&gt; languages = [&quot;C&quot;:1,&quot;C++&quot;:3, &quot;Flex&quot;:5,&quot;VB&quot;:4,&quot;Java&quot;:5].withDefault{10}<\/p>\n<p>println languages.size()<br \/>\n\/\/Output : 5<\/p>\n<p>println languages.get(&quot;Groovy&quot;)<br \/>\n\/\/Output : 10<\/p>\n<p>println languages.get(&quot;COBOL&quot;)<br \/>\n\/\/Output : 10<\/p>\n<p>println languages.size()<br \/>\n\/\/Output : 7<\/p>\n<p>println languages<br \/>\n\/\/Output : [C:1, C++:3, Flex:5, VB:4, Java:5, Groovy:10, COBOL:10]<\/p>\n<p>[\/java]<\/p>\n<p>A new entry gets added in the map for the key whose entry (key-value pair) does not exist in Map with default value.<\/p>\n<p>The only difference between <strong>withLazyDefault<\/strong> and <strong>withEagerDefault<\/strong> is that the gap between actual list and new list will be populated with default value in case of withEagerDefault instead of null values as in case of withLazyDefault method.<\/p>\n<p>In case of <strong>withLazyDefault<\/strong>:<\/p>\n<p>[java]<br \/>\nList&lt;String&gt; languages = [&quot;C&quot;,&quot;C++&quot;,null, &quot;Flex&quot;,&quot;VB&quot;,&quot;Java&quot;].withLazyDefault{&quot;Groovy&quot;}<\/p>\n<p>println languages.get(10)<br \/>\n\/\/ Output : Groovy<\/p>\n<p>println languages<br \/>\n\/\/Output: [C, C++, null, Flex, VB, Java, null, null, null, null, Groovy]<br \/>\n[\/java]<\/p>\n<p>In case of <strong>withEagerDefault<\/strong>:<\/p>\n<p>[java]<br \/>\nList&lt;String&gt; languageList = [&quot;C&quot;,&quot;C++&quot;,null, &quot;Flex&quot;,&quot;VB&quot;,&quot;Java&quot;].withEagerDefault{&quot;Groovy&quot;}<\/p>\n<p>println languageList.get(10)<br \/>\n\/\/Output : Groovy<\/p>\n<p>println languageList \/\/ Output: [C, C++, null, Flex, VB, Java, Groovy, Groovy, Groovy, Groovy, Groovy]<br \/>\n[\/java]<\/p>\n<p>Hope it would help!!!<\/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>Sometimes there is need to get some default value in place of null values from a list, that was the reason I came across: withLazyDefault and withEagerDefault methods. My use case consisted of a list of user names with possibility of null elements, hence wherever the element was null, I wanted to return \u201cunknown\u201d. Although [&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":7},"categories":[7],"tags":[9,977,979,978],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/7621"}],"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=7621"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/7621\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=7621"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=7621"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=7621"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}