Table of Contents

~~SLIDESHOW~~

Lists, Maps and Ranges

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.

Lists

A structure used to store an ordered collection of data items.

List literals

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 Strings
[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)


1 | Example 1: List literals (at-m42/Examples/lecture03/example1.groovy)
extern> http://www.cpjobling.org.uk/~eechris/at-m42/Examples/lecture03/example1.groovy

Notes

List methods

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.


1 | Example 2: List methods (at-m42/Examples/lecture03/example2.groovy)
extern> http://www.cpjobling.org.uk/~eechris/at-m42/Examples/lecture03/example2.groovy

Maps

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 Maps

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)


1 | Example 3: Map literals (at-m42/Examples/lecture03/example3.groovy)
extern> http://www.cpjobling.org.uk/~eechris/at-m42/Examples/lecture03/example3.groovy

Notes

Map methods

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.


1 | Example 4: Map methods (at-m42/Examples/lecture03/example4.groovy)
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:

Ranges

1
// 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 Strings) or Integers. 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.

1 | Example 5: Range methods (at-m42/Examples/lecture03/example5.groovy)
extern> http://www.cpjobling.org.uk/~eechris/at-m42/Examples/lecture03/example5.groovy

Summary of this Lecture

The basic collection classes:

Lab Exercises


Home | Previous Lecture | Lectures | Next Lecture

1)
Those familiar with Java may assume that Lists 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.