Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 27 Next »

Groovy has native language support for collections, lists, maps and arrays.


You can create lists as follows. Notice that [] is the empty list expression.

Each list expression creates an implementation of java.util.List.

See Lists and Sets for more information on using Lists.


Ranges allow you to create a list of sequential values. These can be used as Lists since

Error rendering macro 'link' : Link needs a name and a URL as arguments.
extends java.util.List
Ranges defined with the .. notation are inclusive (that is the list contains the from and to value).
Ranges defined with the ..< notation are exclusive, they include the first value but not the last value.

Note that ranges are implemented efficiently, creating a lightweight Java object containing a from and to value.

Ranges can be used for any Java object which implements java.lang.Comparable for comparison and also have methods next() and previous() to return the next / previous item in the range.
e.g. you can use Strings in a range

Ranges can be used to iterate using the for statement.

but alternatively you can achieve the same effect, by iterating a range with each method:

Ranges can be also used in the switch statements:


Maps can be created using the following syntax. Notice that [:] is the empty map expression.

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].

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.

Note: by design 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()

See Maps for more information on using maps.

Getting efficient with the star-dot '*.' operator

You can perform operations on all the members of a collection using the '*.' operator, e.g.:

Slicing with the subscript operator

You can index into Strings, Lists, arrays, Maps, regexs and such like using the subscript expression.

Notice that you can use ranges to extract part of a List/array/String/regex. This is often referred to as slicing in scripting languages like Python. You can also use a list of indexes too.

You can update items using the subscript operator too

You can use negative indices to count from the end of the List, array, String etc.

Also if you use a backwards range (the starting index is greater than the end index) then the answer is reversed.

Dynamic objects (Expandos)

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 closure mechanisms. An Expando is different from a map in that you can provide synthetic methods that you can call on the object.

The player.greeting assignment passes in a closure to execute when greeting() is called on the Expando. Notice that the closure has access to the properties assigned to the Expando, even though these values may change over time, using Groovy's GString "$variableOrProperty" notation.

  • No labels