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

Version 1 Next »


A list is an ordered collection of objects:

Lists can be evaluated as a boolean value:

We can use [] to assign a new empty list and << to append items to it:

We can add to a list in many ways:

We can use the each and eachWithIndex methods to execute code on each item in a list:

We can construct a list using another's elements as a template:

We can perform a closure on each item of a list and return the result:

Other methods on a list return a value:

We can find the maximum and minimum in a collection:

We can use a "Comparator" to define the comparing behaviour:

We can remove elements from a list by referring to the element/s to be removed:

We can remove an element by referring to its index:

We can remove the first occurrence of an element from a list:

We can clear a list of all elements:

We can pop the last item from a list, and use the list as a simple stack:

Other useful operators and methods:

There's various ways of sorting:

We can repeat a list or element:

We can find the first or last index of items in a list:

Some very common methods are:

A list may contain itself, but equals() may not always be consistent. Consider this:

Ranges and List-Slicing

Ranges are consecutive lists of sequential values like Integers, and can be used just like a List:

They can also be used with single-character strings:

Ranges are handy with the each method:

We can define lists using a range or ranges within a list. This is called slicing:

Lists can be used as subscripts to other lists:

We can view a sublist of a list:

We can perform the same methods on the subscripted lists as we can on the lists they're produced from:

We can update items using subscripting too:

We can also use a method instead of [] with ranges:

More List Utilities

To reverse a list:

We can perform a binary search on a sorted list:

We can remove or retain elements in bulk. retainAll() gives the intersection of two lists; removeAll() gives the assymmetric difference.

Some miscellaneous methods:


A set is an unordered collection of objects, with no duplicates. It can be considered as a list with restrictions, and is often constructed from a list:

A set should not contain itself as an element.

Most methods available to lists, besides those that don't make sense for unordered items, are available to sets.

The add() and addAll() methods return false if the set wasn't changed as a result of the operation:

Examples with Lists and Sets

For small numbers of items, it's common in Groovy to use a list for set processing, and only convert it to a set when necessary, eg, for output.

Though the uniqueness of set items is useful for some processing, for example, if we want to separate the unique and duplicating items in a list:

To calculate the symmetric set difference of two sets non-destructively:


A sorted set is one with extra methods that utilize the sorting of the elements. It's often more efficient than doing the same with lists.

We can construct a TreeSet by giving a comparator to order the elements in the set:

The range-views, headSet() tailSet() and subSet(), are useful views of the items in a sorted set. These range-views remain valid even if the backing sorted set is modified directly. The sorted set returned by these methods will throw an IllegalArgumentException if the user attempts to insert an element out of the range.

For a SortedSet of strings, we can append '\0' to a string to calculate the next possible string:

To go one element backwards from an element elt in a SortedSet:

Immutable Collections

We can convert a list or set into one that can't be modified:

We can create an empty list or set that can't be modified:

We can create a single-element list that can't be modified:


Each ArrayList instance has a capacity, the size of a fixed-size array used to store the elements. This array is always at least as large as the list size, and its capacity grows automatically as elements are added to the list. To see the internal capacity of lists constructed with various values:

For efficiency, we can increase the capacity of a list before adding a large number of elements:

We can see how many times a list has been modified:

We can create a HashSet with initial implementation values:

  • No labels