Skip to content
Skip to breadcrumbs
Skip to header menu
Skip to action menu
Skip to quick search
Quick Search
Browse
Pages
Blog
Labels
Attachments
Mail
Advanced
What’s New
Space Directory
Feed Builder
Keyboard Shortcuts
Confluence Gadgets
Log In
Dashboard
Groovy
Copy Page
You are not logged in. Any changes you make will be marked as
anonymous
. You may want to
Log In
if you already have an account. You can also
Sign Up
for a new account.
This page is being edited by
.
Paragraph
Paragraph
Heading 1
Heading 2
Heading 3
Heading 4
Heading 5
Heading 6
Preformatted
Quote
Bold
Italic
Underline
More colours
Strikethrough
Subscript
Superscript
Monospace
Clear Formatting
Bullet list
Numbered list
Outdent
Indent
Align left
Align center
Align right
Link
Table
Insert
Insert Content
Image
Link
Attachment
Symbol
Emoticon
Wiki Markup
Horizontal rule
tinymce.confluence.insert_menu.macro_desc
Info
JIRA Issue
Status
Gallery
Tasklist
Table of Contents
Other Macros
Page Layout
No Layout
Two column (simple)
Two column (simple, left sidebar)
Two column (simple, right sidebar)
Three column (simple)
Two column
Two column (left sidebar)
Two column (right sidebar)
Three column
Three column (left and right sidebars)
Undo
Redo
Find/Replace
Keyboard Shortcuts Help
<p>Groovy has native language support for collections, lists, maps and arrays.</p><h2>Lists</h2><p>You can create lists as follows. Notice that [] is the empty list expression.</p><table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre>def list = [5, 6, 7, 8] assert list.get(2) == 7 assert list[2] == 7 assert list instanceof java.util.List def emptyList = [] assert emptyList.size() == 0 emptyList.add(5) assert emptyList.size() == 1 </pre></td></tr></table><p>Each list expression creates an implementation of <a href="http://java.sun.com/j2se/1.4.2/docs/api/java/util/List.html">java.util.List</a>.</p><p>See <a href="http://groovy.codehaus.org/JN1015-Collections">Lists and Sets</a> for more information on using Lists.</p><h2>Ranges</h2><p>Ranges allow you to create a list of sequential values. These can be used as Lists since <a href="http://groovy.codehaus.org/apidocs/groovy/lang/Range.html">Range</a> extends <a href="http://java.sun.com/j2se/1.4.2/docs/api/java/util/List.html">java.util.List</a>.<br /> Ranges defined with the <em>..</em> notation are inclusive (that is the list contains the from and to value).<br /> Ranges defined with the <em>..<</em> notation are half-open, they include the first value but not the last value.</p><table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre>// an inclusive range def range = 5..8 assert range.size() == 4 assert range.get(2) == 7 assert range[2] == 7 assert range instanceof java.util.List assert range.contains(5) assert range.contains(8) // lets use a half-open range range = 5..<8 assert range.size() == 3 assert range.get(2) == 7 assert range[2] == 7 assert range instanceof java.util.List assert range.contains(5) assert ! range.contains(8) //get the end points of the range without using indexes def range = 1..10 assert range.from == 1 assert range.to == 10 </pre></td></tr></table><p>Note that ranges are implemented efficiently, creating a lightweight Java object containing a from and to value.</p><p>Ranges can be used for any Java object which implements <a href="http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Comparable.html">java.lang.Comparable</a> for comparison and also have methods next() and previous() to return the next / previous item in the range.<br /> e.g. you can use Strings in a range</p><table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre>// an inclusive range def range = 'a'..'d' assert range.size() == 4 assert range.get(2) == 'c' assert range[2] == 'c' assert range instanceof java.util.List assert range.contains('a') assert range.contains('d') assert ! range.contains('e') </pre></td></tr></table><p>Ranges can be used to iterate using the for statement.</p><table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre>for (i in 1..10) { println "Hello ${i}" } </pre></td></tr></table><p>but alternatively you can achieve the same effect, by iterating a range with <em>each</em> method:</p><table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre>(1..10).each { i -> println "Hello ${i}" } </pre></td></tr></table><p>Ranges can be also used in the <code>switch</code> statements:</p><table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre>switch (years) { case 1..10: interestRate = 0.076; break; case 11..25: interestRate = 0.052; break; default: interestRate = 0.037; } </pre></td></tr></table><h2>Maps <img class="editor-inline-macro" src="/plugins/servlet/confluence/placeholder/macro?definition=e2FuY2hvcjptYXBzfQ&locale=en_GB&version=2" data-macro-name="anchor" data-macro-default-parameter="maps"></h2><p>Maps can be created using the following syntax. Notice that [:] is the empty map expression.</p><p>Map keys are strings by default: [a:1] is equivalent to ["a":1]. But if you really want a variable to become the key, you have to wrap it between parentheses: [(a):1].</p><table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre>def map = [name:"Gromit", likes:"cheese", id:1234] assert map.get("name") == "Gromit" assert map.get("id") == 1234 assert map["name"] == "Gromit" assert map['id'] == 1234 assert map instanceof java.util.Map def emptyMap = [:] assert emptyMap.size() == 0 emptyMap.put("foo", 5) assert emptyMap.size() == 1 assert emptyMap.get("foo") == 5 </pre></td></tr></table><p>Maps also act like beans so you can use the property notation to get/set items inside the Map provided that the keys are Strings which are valid Groovy identifiers.</p><table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre>def map = [name:"Gromit", likes:"cheese", id:1234] assert map.name == "Gromit" assert map.id == 1234 def emptyMap = [:] assert emptyMap.size() == 0 emptyMap.foo = 5 assert emptyMap.size() == 1 assert emptyMap.foo == 5 </pre></td></tr></table><p>Note: by design map.foo will always look for the key foo in map. This means foo.class will return null on an empty map and not result in calling the method getClass()</p><p>See <a href="http://groovy.codehaus.org/JN1035-Maps">Maps</a> for more information on using maps.</p><h2>Getting efficient with the star-dot '*.' operator</h2><p>You can perform operations on all the members of a collection using the '*.' operator, e.g.:</p><table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre>assert [1, 3, 5] == ['a', 'few', 'words']*.size() </pre></td></tr></table><h2>Enhanced Collection Methods</h2><p>In addition to providing the literal syntax for collections, Groovy adds some additional methods to make working with collections more convenient. As an example, you can find big words from a list as follows:</p><table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre>def words = ['ant', 'buffalo', 'cat', 'dinosaur'] assert words.findAll{ w -> w.size() > 4 } == ['buffalo', 'dinosaur'] </pre></td></tr></table><p>Or you can find the first letters of some words as follows:</p><table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre>def words = ['ant', 'buffalo', 'cat', 'dinosaur'] assert words.collect{ it[0] } == ['a', 'b', 'c', 'd'] </pre></td></tr></table><p>In addition to findAll and collect shown above, you have methods like findIndexOf, grep, any, every, min, max, flatten, intersect, disjoint, sort, join and others. Simply look up the <a href="http://groovy.codehaus.org/groovy-jdk/">GDK doco</a> for more details. You might want to look up the added methods for Collection, List and Object to start with.</p><p>Some more details about these methods can also be found in the Quick Start Guide under <a class="confluence-link" href="/display/GROOVY/JN1015-Collections" data-linked-resource-id="75027" data-linked-resource-type="page" data-linked-resource-default-alias="JN1015-Collections" data-base-url="http://docs.codehaus.org">JN1015-Collections</a>.</p><h2>Slicing with the subscript operator</h2><p>You can index into Strings, Lists, arrays, Maps, regexs and such like using the subscript expression.</p><table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre>def text = "nice cheese gromit!" def x = text[2] assert x == "c" assert x.class == String def sub = text[5..10] assert sub == 'cheese' def map = [name:"Gromit", likes:"cheese", id:1234] assert map["name"] == "Gromit" assert map.name == "Gromit" def list = [10, 11, 12] def answer = list[2] assert answer == 12 </pre></td></tr></table><p>Notice that you can use ranges to extract part of a List/array/String/regex. This is often referred to as <em>slicing</em> in scripting languages like Python. You can also use a list of indexes too.</p><table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre>def list = 100..200 def sub = list[1, 3, 20..25, 33] assert sub == [101, 103, 120, 121, 122, 123, 124, 125, 133] </pre></td></tr></table><p>You can update items using the subscript operator too</p><table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre>def list = ["a", "b", "c"] list[2] = "d" list[0] = list[1] list[3] = 5 assert list == ["b", "b", "d", 5] </pre></td></tr></table><p>You can use negative indices to count from the end of the List, array, String etc.</p><table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre>def text = "nice cheese gromit!" def x = text[-1] assert x == "!" def name = text[-7..-2] assert name == "gromit" </pre></td></tr></table><p>Also if you use a backwards range (the starting index is greater than the end index) then the answer is reversed.</p><table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre>def text = "nice cheese gromit!" def name = text[3..1] assert name == "eci" </pre></td></tr></table><h2>Dynamic objects (Expandos)</h2><p>The Expando is not a collection in the strictest sense, but in some ways it is similar to a Map, or objects in JavaScript that do not have to have their properties defined in advance. It allows you to create dynamic objects by making use of Groovy's <a class="confluence-link" href="/display/GROOVY/Closures+-+Informal+Guide" data-linked-resource-id="2729" data-linked-resource-type="page" data-linked-resource-default-alias="Closures - Informal Guide" data-base-url="http://docs.codehaus.org">closure mechanisms</a>. An <a href="http://groovy.codehaus.org/gapi/groovy/util/Expando.html">Expando</a> is different from a map in that you can provide synthetic methods that you can call on the object.</p><table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre>def player = new Expando() player.name = "Dierk" player.greeting = { "Hello, my name is $name" } println player.greeting() player.name = "Jochen" println player.greeting() </pre></td></tr></table><p>The player.greeting assignment passes in a <a class="confluence-link" href="/display/GROOVY/Closures+-+Informal+Guide" data-linked-resource-id="2729" data-linked-resource-type="page" data-linked-resource-default-alias="Closures - Informal Guide" data-base-url="http://docs.codehaus.org">closure</a> to execute when greeting() is called on the <a href="http://groovy.codehaus.org/gapi/groovy/util/Expando.html">Expando</a>. Notice that the closure has access to the properties assigned to the <a href="http://groovy.codehaus.org/gapi/groovy/util/Expando.html">Expando</a>, even though these values may change over time, using Groovy's <a class="confluence-link" href="/display/GROOVY/Strings+and+GString" data-linked-resource-id="2771" data-linked-resource-type="page" data-linked-resource-default-alias="Strings and GString" data-base-url="http://docs.codehaus.org">GString</a> "$variableOrProperty" notation.</p>
Please type the word appearing in the picture.
Attachments
Labels
Location
Watch this page
< Edit
Preview >
Loading…
Save
Cancel
Next hint
search
attachments
weblink
advanced