As our first part of seeing how the collections work, we'll look at constructing collections. You may be surprised to learn that there are actually quite a number of different ways of constructing collections. That's enough preamble. Let's get into it. The simplest way to construct a collection is to create an empty collection. Let's see some examples. To construct an empty list, we simply call the empty method on the list companion object. To construct a mutable.ArrayBuffer, we call the empty method on the ArrayBuffer companion object. It'd be no surprise to learn that to construct an empty Map, we call the empty method on the Map companion object. You may have noticed on the previous slide that Scala inferred the collection element as being of type Nothing. This isn't very useful because we can't create any values with that type. There is no value that have the type Nothing. What we usually want to do is specify the element type when we call the empty method. We have some examples here, calling List.empty with Int, ArrayBuffer.empty with double, and Map.empty with string and boolean, where we have two types. The first type is the type of the key, and the second type is the type of the values we store in the map. That gives us a collection with a useful type for the element values. Creating an empty collection is not always the thing that we want to do. Sometimes you already have the elements to put in the collection. It'd be nice if we could construct a collection directly with those elements inside it. We've actually already seen how to do this for list. We call the constructor like so. The same thing works for ArrayBuffer as well. We can call ArrayBuffer, with the elements we want in it and with an ArrayBuffer constructed with those elements already inside it. This is sometimes known as a vararg constructor. Vararg mean a variable number of arguments or parameters that we pass to the method so we can pass as many elements as we want here. It's not just like a normal function, where you can only pass a set number of elements as parameters. When we construct a Map using the vararg constructor, we must specify both the keys and the values associated with them. That's what we do in this example here. We have a key a associated with the value true, a key e associated with the value true, and a key b associated with the value false. In this case, the arrow is indicating the association. Now we haven't seen this syntax before so what is it doing? What it does is it constructs what's called a tuple, sometimes pronounced tuple. They're both fine. We haven't learned anything about tuples yet, so let's have a brief detour to learn a little bit about them. A tuple is in some ways the opposite of a list, whereas a list holds a variable number of elements, but they all have to have the same type. A tuple holds a fixed number of elements, but these elements can have different types. We've already seen the syntax for constructing tuple of two elements with arrow syntax. This arrow syntax is a shorthand for the more general form of constructing tuples, which is using parentheses, with the elements separated by commas. This extends to three or more elements. Here's some examples. Firstly, we construct a tuple of two elements using the arrow syntax we've already seen. Then we construct a tuple of the same elements, but this time using the parentheses syntax. This parenthesis syntax extends to three or more elements as shown here. Remember, separate the elements with commas and surround them by parentheses. When we come to write the types of tuples, it's got the same form. We specify the types, separate it by commas and surrounded by parentheses. We can also pattern match on tuples. The syntax for patterns on tuples follows the same general principle we've seen so far, which is that the pattern for deconstructing something look similar to the code we'd use to construct it. For tuples, this means pattern separated by commas surrounded by parentheses. Here's an example. We have a tuple of two elements, and we can pattern match on it using this pattern here. Number; the name number is given to the first element which is 10, and the name greeting is given to the second element, which is hello. Another thing we can do with pattern matching, is put them in a val like so. We don't usually do this. This is not the most common thing to do, but it's very convenient to be able to deconstruct a tuple in this way. As well as accessing the elements of a tuple by using pattern matching, we can also access them directly by index. The indices start at zero. Here's an example. Let's say we have a tuple of two elements shown here, Alice and a number 42. We can access the first element using the index 0 by calling it as if it of a function. We can access the second element which has index 1 by doing the same thing. If we already have a collection, we can add elements to the front and back of the collection to create a new collection. The method for adding an element to the front is this plus colon method, and the method of adding element to the back is the opposite, colon plus. Here's an example. First, we add element to the front of the list. You can see we get the list 0, 1, 2, 3, the element zero added to the front. Here we have a mutable.ArrayBuffer and we add an element to the end using the colon plus method. We see the result, c has been added to the end. We cannot use the colon plus and plus colon methods to add elements to a Map because a Map does not have any notion of ordering, the elements aren't in any particular order. Instead, we just use the plus method to add elements. Remember, when we add an element, we have to specify the key and the value, which we do using a tuple. That's it for constructing collections. Let's recap what we've seen so far. We can construct an empty collection by calling the empty method on a companion object. We may need to specify the element type explicitly if Scala does not infer it. We can call the vararg constructor to construct a collection with any number of elements. We can construct a new collection from an existing collection by adding elements to the beginning or end of that collection. The methods to do so are the plus colon and colon plus methods, which prepend and append elements respectively. This doesn't work for Map because Map has no notion of ordering in its elements. Instead, we use the plus method. Remember that these methods are constructing new collections. They don't modify the existing collection when they do so. We have also learned about tuples, which allows us to store a fixed number of elements but of varying type. We've seen lots of ways of interacting with tuples, including pattern matching and accessing elements by index.