List
s are the same as Java arrays. They look the same but they are very different structures! In fact the Groovy List
unifies the syntax and behaviour of the Java array and the List class.~~SLIDESHOW~~
Lists, maps and ranges are collection classes: they each hold references to other objects.
The slides and notes in this presentation are adapted from Groovy Programming (See Recommended Reading).
An index to the source code for all the examples in this lecture is available.
The List
and Map
can reference objects of differing types. The Range
represents a collection of integer values. List
and Map
are dynamic and can grow and shrink at run-time. Each object in a List
is indexed by an integer index. A Map
collection can be indexed by any kind of object (although more often than not a String
is used). Both List
and Map
can contain other lists and maps, so you can build data structures of arbitrary complexity.
A structure used to store an ordered collection of data items.
A List
literal is presented as a series of objects separated by commas and enclosed in square brackets1)
Example | Description |
---|---|
[97, 98, 99, 100] | A list of integer values |
['Chris', 'Paul', 'Robert' ] | A list of String s |
[1, 2, [3, 4], 5] | A nested list |
['Chris', 51, 1.76] | A heterogenous list of object references |
[ ] | an empty list |
See code example 1 (listed in notes)
extern> http://www.cpjobling.org.uk/~eechris/at-m42/Examples/lecture03/example1.groovy
Notes
[ ]
which is implemented using the overloaded method getAt
. The first element in a list is index zero. In the example line 1 declares a list of numbers and lines 5 and 6 show the use of indexing to retrieve values.List
using ranges (see Ranges) as shown in lines 12 to 13.List
indexing operator can also be used to set new values in a List
as shown in line 16. You can also add a list to a list, as shown in line 18. The inserting of items to a list is implemented using the method putAt
.List
using the «
operator (leftShift
method) as shown on line 22.plus
method has been overloaded for lists so that you can add new items using the +
operator (line 23).minus
method has been overloaded for lists so that you can remove items using the -
operator (line 26).
Like the String
object, the List
object has all the methods supported by java.util.List as well as a number of additional methods added by Groovy.
Example 2 (see listing in the notes) gives some examples of the more commonly used List
methods.
extern> http://www.cpjobling.org.uk/~eechris/at-m42/Examples/lecture03/example2.groovy
Also known as associative array, dictionary, table and hash.
The keys used in a Map
can be of any class. When we insert a new element into a Map
we need two values; a key and a value. indexing the Map
with the same key retrieves the value.
Example | Description |
---|---|
['Chris' : 'Jobling', 'Javier': 'Bonet'] | Forename/surname collection |
[4 : [2], 6 : [3, 2], 12 : [6, 4, 3, 2]] | Integer keys and their list of divisors |
[ : ] | Empty map |
See code example 3 (listed in notes)
extern> http://www.cpjobling.org.uk/~eechris/at-m42/Examples/lecture03/example3.groovy
Notes
Map
literals comprise a comma separated list of key:value
pairs enclosed in a square brackets.Map
are accessed using the subscript operator (implemented using the getAt
method). The index value can be any class of object that represents the key (line 6). The value returned is the value associated with the key (line 6) or null
(line 8) if no value with that key exists.String
keys, you can also use the property form of reference (line 7).putAt
method is used for updating elements and the notation for assignment is the same as for lists (line 12).
The Map
object has all the methods supported by java.util.Map as well as a number of additional methods added by Groovy.
Example 4 (see listing in the notes) gives some examples of the more commonly used Map
methods.
extern> http://www.cpjobling.org.uk/~eechris/at-m42/Examples/lecture03/example4.groovy
The examples are fairly self-explanatory. A couple of points of note:
keySet
returns an item of type KeySet
(that is a Set
), this is possible because each key value must be unique. values
returns a Collection
object, this is more conveniently handled as a list so the idiom is mp.getValues().asList()
(line 30).assert
statements. I used sort
on line 31 to ensure the order of the values would be predictable.// Ranges 1900..1999 // twentieth century (inclusive Range) 2000..<2100 // twenty-first century (exclusive Range) 'A'..'D' // A, B, C, and D 10..1 // 10, 9, 8, ..., 1 'Z'..'X' // Z, Y and X // using expressions in ranges def start = 10 def finish = 20 start..finish+1 // 10, 11, 12, ...., 20, 21
Example 5 (see listing in the notes) gives some examples of the more commonly used Range
methods.
A range is a shorthand for specifying a sequence of values. A Range
is denoted by the first and last values in the sequence, and Range
can be inclusive or exclusive. An inclusive range includes all values from the first to the last (lines 2 and 4 in the slide). Note that ..
is used for an inclusive range and ..<
for an exclusive range (line 3). A range can be denoted by characters (single letter String
s) or Integer
s. As shown in lines 5 and 6, ranges can be ascending or descending.
More examples of the Range
methods are given below.
The first and last value for a Range
can be any numeric integer expression as shown in lines 9–11.
extern> http://www.cpjobling.org.uk/~eechris/at-m42/Examples/lecture03/example5.groovy
List
s are the same as Java arrays. They look the same but they are very different structures! In fact the Groovy List
unifies the syntax and behaviour of the Java array and the List class.