Message-ID: <196788767.2339.1412254179057.JavaMail.email@example.com> Subject: Exported From Confluence MIME-Version: 1.0 Content-Type: multipart/related; boundary="----=_Part_2338_899790363.1412254179056" ------=_Part_2338_899790363.1412254179056 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Content-Location: file:///C:/exported.html
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 it= em 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:= p>
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:= p>
We can remove elements from a list by referring to the element/s to be r= emoved:
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. Co= nsider this:
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 calle= d 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 th= e lists they're produced from:
We can update items using subscripting too:
We can also use a method instead of  with ranges:
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 intersec= tion 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:
Here are a few common set theory concepts and how to make them work in G= roovy.
Two sets, A and B, and a third set, C, where C combines all distinct elements of both A a= nd B. Set C would be a union of A and B. Use the '+' operator to c= reate a union between two sets.
Two sets, A and B, and a third set, C, where C combines all elements of = A and B except the items existing in both A and B. Set C w= ould be a symmetric difference of A and B. Here is the original ex= ample of symmetric difference:
Two sets, A and B, and a third set, C, where C combines only elements of= A and B that exist in both A and B. Set C would be an intersection of A and B. The intersect method is actually a part of the
Two sets, A and B, and a third set, C, where C contains all elements fro= m A that do not also exist in B. Set C would contain the compliment of A in= B. Said differently, set C shows what A has to offer B that B doesn't alre= ady have.
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 ex= ample, if we want to separate the unique and duplicating items in a list:= p>
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 e= lements. 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 t= he items in a sorted set. These range-views remain valid even if the backin= g sorted set is modified directly. The sorted set returned by these methods= will throw an IllegalArgumentException if the user attempts to insert an e= lement 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:
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: