{"id":464,"date":"2007-12-12T00:00:00","date_gmt":"2007-12-11T16:00:00","guid":{"rendered":"http:\/\/www.strongd.net\/?p=464"},"modified":"2011-07-15T09:46:33","modified_gmt":"2011-07-15T01:46:33","slug":"your-first-groovy","status":"publish","type":"post","link":"https:\/\/www.strongd.net\/?p=464","title":{"rendered":"Your First Groovy"},"content":{"rendered":"<p><DIV><br \/>\n<DIV class=section_2><br \/>\n<H2><A name=QuickStart-YourFirstGroovy><\/A>Your First Groovy<\/H2><br \/>\n<DIV class=code><br \/>\n<DIV class=codeContent><PRE class=code-java><SPAN class=code-comment><FONT color=#808080>\/\/hello.groovy<br \/>\n<\/FONT><\/SPAN>println <SPAN class=code-quote><FONT color=#009100>&#8220;hello, world&#8221;<BR><\/FONT><\/SPAN><br \/>\n<SPAN class=code-keyword><FONT color=#000091>for<\/FONT><\/SPAN> (arg in <SPAN class=code-keyword><FONT color=#000091>this<\/FONT><\/SPAN>.args ) {<BR><br \/>\n  println <SPAN class=code-quote><FONT color=#009100>&#8220;Argument:&#8221;<\/FONT><\/SPAN> + arg;<BR><br \/>\n}<BR><br \/>\n<SPAN class=code-comment><FONT color=#808080>\/\/ <\/FONT><SPAN class=code-keyword><FONT color=#000091>this<\/FONT><\/SPAN><FONT color=#808080> is a comment<BR><br \/>\n<\/FONT><\/SPAN>\/* a block comment, commenting out an alternative to above:<BR><br \/>\n<SPAN class=code-keyword><FONT color=#000091>this<\/FONT><\/SPAN>.args.each{ arg -&gt; println <SPAN class=code-quote><FONT color=#009100>&#8220;hello, ${arg}&#8221;<\/FONT><\/SPAN>}<BR><br \/>\n*\/<BR><\/PRE><\/DIV><\/DIV><br \/>\n<P>To run it from command line<\/P><br \/>\n<DIV class=code><br \/>\n<DIV class=codeContent><PRE class=code-java>groovy hello.groovy MyName yourName HisName<\/PRE><\/DIV><\/DIV><\/DIV><br \/>\n<DIV class=section_2><br \/>\n<H2><A name=QuickStart-Overview><\/A>Overview<\/H2><br \/>\n<P>Groovy classes compile down to Java bytecode and so there&#8217;s a 1-1 mapping between a Groovy class and a Java class.<BR><BR>Indeed each Groovy class can be used inside normal Java code &#8211; since it is a Java class too.<\/P><br \/>\n<P>Probably the easiest way to get groovy is to try working with collections. In Groovy List (java.util.List) and Map (java.util.Map) are both first class objects in the syntax. So to create a List of objects you can do the following&#8230;<\/P><br \/>\n<DIV class=code><br \/>\n<DIV class=codeContent><PRE class=code-java>def list = [1, 2, &#8216;hello&#8217;, <SPAN class=code-keyword><FONT color=#000091>new<\/FONT><\/SPAN> java.util.Date()]<BR><br \/>\n<SPAN class=code-keyword><FONT color=#000091>assert<\/FONT><\/SPAN> list.size() == 4<BR><br \/>\n<SPAN class=code-keyword><FONT color=#000091>assert<\/FONT><\/SPAN> list.get(2) == &#8216;hello&#8217;<BR><br \/>\n<SPAN class=code-keyword><FONT color=#000091>assert<\/FONT><\/SPAN> list[2] == &#8216;hello&#8217;<BR><\/PRE><\/DIV><\/DIV><br \/>\n<P>Notice that everything is an object (or that auto-boxing takes place when working with numbers). To create maps&#8230;<\/P><br \/>\n<DIV class=code><br \/>\n<DIV class=codeContent><PRE class=code-java>def map = [&#8216;name&#8217;:&#8217;James&#8217;, &#8216;location&#8217;:&#8217;London&#8217;]<BR><br \/>\n<SPAN class=code-keyword><FONT color=#000091>assert<\/FONT><\/SPAN> map.size() == 2<BR><br \/>\n<SPAN class=code-keyword><FONT color=#000091>assert<\/FONT><\/SPAN> map.get(&#8216;name&#8217;) == &#8216;James&#8217;<BR><br \/>\n<SPAN class=code-keyword><FONT color=#000091>assert<\/FONT><\/SPAN> map[&#8216;name&#8217;] == &#8216;James&#8217;<BR><\/PRE><\/DIV><\/DIV><br \/>\n<P>Iterating over collections is easy&#8230;<BR><\/P><br \/>\n<DIV class=code><br \/>\n<DIV class=codeContent><PRE class=code-java>def list = [1, 2, 3]<BR><br \/>\n<SPAN class=code-keyword><FONT color=#000091>for<\/FONT><\/SPAN> (i in list) { println i }<BR><\/PRE><\/DIV><\/DIV><br \/>\n<P>Once you have some collections you can then use some of the new collection helper methods or try working with closures&#8230;<\/P><\/DIV><br \/>\n<DIV class=section_2><br \/>\n<H2><A name=QuickStart-Workingwithclosures><\/A>Working with closures<\/H2><br \/>\n<P>Closures are similar to Java&#8217;s inner classes, except they are a single method which is invokable, with arbitrary parameters. A closure can have as many parameters as you wish&#8230;<\/P><br \/>\n<DIV class=code><br \/>\n<DIV class=codeContent><PRE class=code-java>def closure = { param -&gt; println(<SPAN class=code-quote><FONT color=#009100>&#8220;hello ${param}&#8221;<\/FONT><\/SPAN>) }<BR><br \/>\nclosure.call(<SPAN class=code-quote><FONT color=#009100>&#8220;world!&#8221;<\/FONT><\/SPAN>)<BR><\/p>\n<p>closure = { greeting, name -&gt; println(greeting + name) }<BR><br \/>\nclosure.call(<SPAN class=code-quote><FONT color=#009100>&#8220;hello &#8220;<\/FONT><\/SPAN>, <SPAN class=code-quote><FONT color=#009100>&#8220;world!&#8221;<\/FONT><\/SPAN>)<BR><\/PRE><\/DIV><\/DIV><br \/>\n<P>If no parameter(s) is(are) specified before -&gt; symbol then a default named parameter, called &#8216;it&#8217; can be used. e.g.<BR><\/P><br \/>\n<DIV class=code><br \/>\n<DIV class=codeContent><PRE class=code-java>def closure = { println <SPAN class=code-quote><FONT color=#009100>&#8220;hello &#8220;<\/FONT><\/SPAN> + it }<BR><br \/>\nclosure.call(<SPAN class=code-quote><FONT color=#009100>&#8220;world!&#8221;<\/FONT><\/SPAN>)<BR><\/PRE><\/DIV><\/DIV><br \/>\n<P>Using closures allows us to process collections (arrays, maps, strings, files, SQL connections and so forth) in a clean way. e.g<BR><\/P><br \/>\n<DIV class=code><br \/>\n<DIV class=codeContent><PRE class=code-java>[1, 2, 3].each ({ item -&gt; print <SPAN class=code-quote><FONT color=#009100>&#8220;${item}-&#8220;<\/FONT><\/SPAN> })<BR><br \/>\n[<SPAN class=code-quote><FONT color=#009100>&#8220;k1&#8221;<\/FONT><\/SPAN>:<SPAN class=code-quote><FONT color=#009100>&#8220;v1&#8221;<\/FONT><\/SPAN>, <SPAN class=code-quote><FONT color=#009100>&#8220;k2&#8221;<\/FONT><\/SPAN>:<SPAN class=code-quote><FONT color=#009100>&#8220;v2&#8221;<\/FONT><\/SPAN>].each {key, value -&gt; println key + <SPAN class=code-quote><FONT color=#009100>&#8220;=&#8221;<\/FONT><\/SPAN> + value}<\/PRE><\/DIV><\/DIV><br \/>\n<P><B>Note:<\/B> If a given closure is the last parameter of a method, its definition can reside outside of the parentheses. Thus the following code is valid:<\/P><br \/>\n<DIV class=code><br \/>\n<DIV class=codeContent><PRE class=code-java>def fun(<SPAN class=code-object><FONT color=#910091>int<\/FONT><\/SPAN> i, Closure c) {<BR><br \/>\n  c.call(i)<BR><br \/>\n}<BR><\/p>\n<p><SPAN class=code-comment><FONT color=#808080>\/\/ put Closure out of ()<BR><br \/>\n<\/FONT><\/SPAN>[1, 2, 3].each() { item -&gt; print <SPAN class=code-quote><FONT color=#009100>&#8220;${item}-&#8220;<\/FONT><\/SPAN> } <SPAN class=code-comment><FONT color=#808080>\/\/ 1-2-3-<BR><br \/>\n<\/FONT><\/SPAN>fun(123) { i -&gt; println i } <SPAN class=code-comment><FONT color=#808080>\/\/ 123<BR><\/FONT><\/SPAN><br \/>\n<SPAN class=code-comment><FONT color=#808080>\/\/ omit ()<\/FONT><\/SPAN><\/PRE><\/DIV><\/DIV><SPAN class=code-comment><FONT color=#808080><\/FONT><\/SPAN><SPAN class=code-comment><FONT color=#808080><br \/>\n<DIV class=code><BR><\/FONT><\/SPAN>[1, 2, 3].each ({ item -&gt; print <SPAN class=code-quote><FONT color=#009100>&#8220;${item}-&#8220;<\/FONT><\/SPAN> }) <SPAN class=code-comment><FONT color=#808080>\/\/ 1-2-3-<\/FONT><\/SPAN><\/DIV><br \/>\n<DIV class=code><SPAN class=code-comment><FONT color=#808080><BR><\/FONT><\/SPAN><BR><SPAN class=code-comment><FONT color=#808080>\/\/ omit enclosing ()<BR><\/FONT><\/SPAN>[1, 2, 3].each { item -&gt; print <SPAN class=code-quote><FONT color=#009100>&#8220;${item}-&#8220;<\/FONT><\/SPAN> } <SPAN class=code-comment><FONT color=#808080>\/\/ 1-2-3-<BR><\/FONT><\/SPAN><BR><SPAN class=code-comment><FONT color=#808080>\/\/ normal<BR><\/FONT><\/SPAN>[1, 2, 3].each(({ item -&gt; print <SPAN class=code-quote><FONT color=#009100>&#8220;${item}-&#8220;<\/FONT><\/SPAN> })) <SPAN class=code-comment><FONT color=#808080>\/\/ 1-2-3-<BR><\/FONT><\/SPAN><BR><SPAN class=code-comment><FONT color=#808080>\/\/ using the fun function to <\/FONT><SPAN class=code-keyword><FONT color=#000091>do<\/FONT><\/SPAN><FONT color=#808080> the same thing<BR><\/FONT><\/SPAN>[1,2,3].each {fun(it,{item -&gt; print <SPAN class=code-quote><FONT color=#009100>&#8220;${item}-&#8220;<\/FONT><\/SPAN>})} <SPAN class=code-comment><FONT color=#808080>\/\/ 1-2-3-<BR><\/FONT><\/SPAN><BR>def closure = { i -&gt; println i}<BR><BR><SPAN class=code-comment><FONT color=#808080>\/\/[1, 2, 3].each() closure \/\/ error. closure has been previously defined<\/FONT><\/SPAN><\/DIV><br \/>\n<P>Here are a number of helper methods available on collections &amp; strings&#8230;<\/P><br \/>\n<DIV class=section_3><br \/>\n<H3><A name=QuickStart-each><\/A>each<\/H3><br \/>\n<P>iterate via a closure<\/P><br \/>\n<DIV class=code><br \/>\n<DIV class=codeContent><PRE class=code-java>[1, 2, 3].each { item -&gt; print <SPAN class=code-quote><FONT color=#009100>&#8220;${item}-&#8220;<\/FONT><\/SPAN> }<\/PRE><\/DIV><\/DIV><\/DIV><br \/>\n<DIV class=section_3><br \/>\n<H3><A name=QuickStart-collect><\/A>collect<\/H3><br \/>\n<P>collect the return value of calling a closure on each item in a collection<\/P><br \/>\n<DIV class=code><br \/>\n<DIV class=codeContent><PRE class=code-java>def value = [1, 2, 3].collect { it * 2 }<BR><br \/>\n<SPAN class=code-keyword><FONT color=#000091>assert<\/FONT><\/SPAN> value == [2, 4, 6]<\/PRE><\/DIV><\/DIV><\/DIV><br \/>\n<DIV class=section_3><br \/>\n<H3><A name=QuickStart-find><\/A>find<\/H3><br \/>\n<P>finds first item matching closure predicate<\/P><br \/>\n<DIV class=code><br \/>\n<DIV class=codeContent><PRE class=code-java>def value = [1, 2, 3].find { it &gt; 1 }<BR><br \/>\n<SPAN class=code-keyword><FONT color=#000091>assert<\/FONT><\/SPAN> value == 2<\/PRE><\/DIV><\/DIV><\/DIV><br \/>\n<DIV class=section_3><br \/>\n<H3><A name=QuickStart-findAll><\/A>findAll<\/H3><br \/>\n<P>finds all items matching closure predicate<\/P><br \/>\n<DIV class=code><br \/>\n<DIV class=codeContent><PRE class=code-java>def value = [1, 2, 3].findAll { it &gt; 1 }<BR><br \/>\n<SPAN class=code-keyword><FONT color=#000091>assert<\/FONT><\/SPAN> value == [2, 3]<\/PRE><\/DIV><\/DIV><\/DIV><br \/>\n<DIV class=section_3><br \/>\n<H3><A name=QuickStart-inject><\/A>inject<\/H3><br \/>\n<P>allows you to pass a value into the first iteration and then pass the result of that iteration into the next iteration and so on. This is ideal for counting and other forms of processing<\/P><br \/>\n<DIV class=code><br \/>\n<DIV class=codeContent><PRE class=code-java>def value = [1, 2, 3].inject(&#8216;counting: &#8216;) { str, item -&gt; str + item }<BR><br \/>\n<SPAN class=code-keyword><FONT color=#000091>assert<\/FONT><\/SPAN> value == <SPAN class=code-quote><FONT color=#009100>&#8220;counting: 123&#8221;<\/FONT><\/SPAN><\/p>\n<p>value = [1, 2, 3].inject(0) { count, item -&gt; count + item }<BR><br \/>\n<SPAN class=code-keyword><FONT color=#000091>assert<\/FONT><\/SPAN> value == 6<\/PRE><\/DIV><\/DIV><br \/>\n<P>In addition there&#8217;s 2 new methods for doing boolean logic on some collection&#8230;<\/P><\/DIV><br \/>\n<DIV class=section_3><br \/>\n<H3><A name=QuickStart-every><\/A>every<\/H3><br \/>\n<P>returns true if all items match the closure predicate<\/P><br \/>\n<DIV class=code><br \/>\n<DIV class=codeContent><PRE class=code-java>def value = [1, 2, 3].every { it &lt; 5 }<BR><br \/>\n<SPAN class=code-keyword><FONT color=#000091>assert<\/FONT><\/SPAN> value<\/p>\n<p>value = [1, 2, 3].every { item -&gt; item &lt; 3 }<BR><br \/>\n<SPAN class=code-keyword><FONT color=#000091>assert<\/FONT><\/SPAN> ! value<\/PRE><\/DIV><\/DIV><\/DIV><br \/>\n<DIV class=section_3><br \/>\n<H3><A name=QuickStart-any><\/A>any<\/H3><br \/>\n<P>returns true if any item match the closure predicate<\/P><br \/>\n<DIV class=code><br \/>\n<DIV class=codeContent><PRE class=code-java>def value = [1, 2, 3].any { it &gt; 2 }<BR><br \/>\n<SPAN class=code-keyword><FONT color=#000091>assert<\/FONT><\/SPAN> value<\/p>\n<p>value = [1, 2, 3].any { item -&gt; item &gt; 3 }<BR><br \/>\n<SPAN class=code-keyword><FONT color=#000091>assert<\/FONT><\/SPAN> value == <SPAN class=code-keyword><FONT color=#000091>false<\/FONT><\/SPAN><\/PRE><\/DIV><\/DIV><br \/>\n<P>Other helper methods include:<\/P><\/DIV><br \/>\n<DIV class=section_3><br \/>\n<H3><A name=QuickStart-max%2Fmin><\/A>max \/ min<\/H3><br \/>\n<P>returns the max\/min values of the collection &#8211; for Comparable objects<\/P><br \/>\n<DIV class=code><br \/>\n<DIV class=codeContent><PRE class=code-java>value = [9, 4, 2, 10, 5].max()<BR><br \/>\n<SPAN class=code-keyword><FONT color=#000091>assert<\/FONT><\/SPAN> value == 10<BR><br \/>\nvalue = [9, 4, 2, 10, 5].min()<BR><br \/>\n<SPAN class=code-keyword><FONT color=#000091>assert<\/FONT><\/SPAN> value == 2<BR><br \/>\nvalue = [&#8216;x&#8217;, &#8216;y&#8217;, &#8216;a&#8217;, &#8216;z&#8217;].min()<BR><br \/>\n<SPAN class=code-keyword><FONT color=#000091>assert<\/FONT><\/SPAN> value == &#8216;a&#8217;<BR><\/PRE><\/DIV><\/DIV><\/DIV><br \/>\n<DIV class=section_3><br \/>\n<H3><A name=QuickStart-join><\/A>join<\/H3><br \/>\n<P>concatenates the values of the collection together with a string value<\/P><br \/>\n<DIV class=code><br \/>\n<DIV class=codeContent><PRE class=code-java>def value = [1, 2, 3].join(&#8216;-&#8216;)<BR><br \/>\n<SPAN class=code-keyword><FONT color=#000091>assert<\/FONT><\/SPAN> value == &#8216;1-2-3&#8217;<\/PRE><\/DIV><\/DIV><\/DIV><\/DIV><\/DIV><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Your First Groovy \/\/hello.groovy println &#8220;hello, world&#8221; for (arg in this.args ) { println &#8220;Argument:&#8221; + arg; } \/\/ this is a comment \/* a block comment, commenting out an alternative to above: this.args.each{ arg -&gt; println &#8220;hello, ${arg}&#8221;} *\/ To run it from command line groovy hello.groovy MyName yourName HisName Overview Groovy classes compile &hellip; <a href=\"https:\/\/www.strongd.net\/?p=464\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Your First Groovy<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[120],"class_list":["post-464","post","type-post","status-publish","format-standard","hentry","category-linux","tag-groovy"],"_links":{"self":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts\/464","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=464"}],"version-history":[{"count":1,"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts\/464\/revisions"}],"predecessor-version":[{"id":906,"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts\/464\/revisions\/906"}],"wp:attachment":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=464"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=464"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=464"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}