Lists in Groovy
In Groovy, Lists come straight out of the Java collection classes but with added syntactic sugar.
The easiest way to create a list is:
def myList = [1, 2, 3]
An empty list can be created by using []
def myEmptyList = []
As Groovy's List is a java.util.List, you can use all the methods on the java.util.List interface. An example would be add:
myList.add(4)
which will add 4 to the end of the list.
You can add an element (or even the contents of another list) to the end of a list with the += operator
myList += 5
myList += [6,7]
will result in myList containing [1, 2, 3, 4, 5, 6, 7]. The + operator works much as you would expect:
def anotherList = [1,2,3] + [4,5,6]
results in [1, 2, 3, 4, 5, 6]. One interesting thing to note is that:
def yetAnotherList = [1, 2, 3]
yetAnotherList.add([4, 5, 6])
results in [1, 2, 3, [4, 5, 6]] and not [1, 2, 3, 4, 5, 6]
To access a particular element of a list, you use square brackets:
println(myList[0])
will print the number 1.
You can an also slice a list using a range:
def subList = myList[2..4]
results in subList containing [3,4,5].
If you create two different lists and they both contain the same elements, == works as you expect:
def list1 = [1,2,3]
def list2 = [1,2,3]
list1 == list2
returns true.
As you have seen, lists in Groovy are mutable - you can add, remove and change the elements to your hearts content. Scala is different.
Lists (and Arrays) in Scala
Unlike Groovy, Scala has both Lists and Arrays. The main difference between them (at least for the purposes of this post) is mutability - Arrays are mutable, Lists are not.
Scala doesn't have a special syntax for either lists or arrays (though it does use a language feature so that you don't need to use new). So, to create a list (or an array) in Scala, you can use:
val myList = List(1, 2, 3)
val myArray = Array(4, 5, 6)
As arrays are mutable, you may wish to create an empty array and populate later:
val myEmptyArray = new Array[Int](3)
which creates an array of size 3 that contains integers. There are a couple of ways to set the values in this array:
myEmptyArray.update(0, 10)
myEmptyArray(1) = 11
myEmptyArray will now contain [10, 11, 0] - when the array was created, each value was initialized to the default "empty" value for that type, which in this case is 0 for Ints.
With lists in Scala, you can't add elements to the list (or delete or change elements either), in the same way that you can't add, delete or change characters in a string. But just like with strings, you can still perform operations on lists, it's just that you get an entirely new list as a result of the operation. We'll start by looking at adding elements:
var aList = List(1, 2, 3)
aList = 0 :: aList
results in aList containing [0, 1, 2, 3]. You'll note how I used var and not val - val would not have worked as we are creating a new list and changing aList to reference the new list. You'll also notice the :: "operator"1 , which is called "cons". Cons creates a new list with the the object on the left hand side of cons as the first element of the new list and the elements of the list on the right hand side making up the rest of the new list.
Scala has a special "shorthand" for creating an empty list, "Nil"
You can use Nil to build up lists. For example:
var bList = 0 :: 1 :: 2 :: 3 :: Nil
results in bList containing [0, 1, 2, 3]. One interesting thing to note is that
aList == bList
will return true, as both lists contain [0, 1, 2, 3].
There is also a ::: operator, which will combine 2 lists into a single new list
var cList = List(1, 2, 3)
var dList = 4 :: 5 :: 6 :: Nil
var longList = cList ::: dList
will result in a list containing [1, 2, 3, 4, 5, 6]
If we wanted to slice longList and get a sub list of elements:
var sliced = longList.slice(2, 4)
results in a list contain [3, 4] and not [3, 4, 5] that the Groovy slice above (using (2..4)) returns.
Conclusion
I've left a lot (and I mean a lot) of info about Lists in both Groovy and Scala out in this post. The reason why I'm introducing them now is that both languages give Strings list-like syntactic sugar. As I've barely scratched the surface of Lists (let alone the rest of the collection classes) I will get back to them in greater detail later.
Footnotes:
1Why operator is in double quotes will be the subject of a future comparison of operators and operator overloading