Get startedGet started for free

Lists

1. Lists

Now, another collection to add to your list: List. Lists are probably the most commonly used collection in Scala.

2. Collections

Again, there are two kinds of collections: mutable and immutable. You've used Array, which is a mutable sequence of objects that share the same type. Mutable meaning you can change, add, or remove elements of an Array, though you can't change the parameter values like the length of the array. List is an immutable sequence of objects that share the same type. Like with Array, "share the same type" unintuitively means you can have mixed types when you use the Any supertype.

3. Lists have a type parameter

Lists, like Arrays, have a type parameter specifying what the List holds. Say if you knew the players in Twenty-One would never change ever, you might use List: val players equals List then the names. players here is type List String. Note that Scala Lists are ALWAYS immutable, unlike in Java, where they can be mutable.

4. How Lists are useful while immutable

A List is an object, like pretty much everything in Scala, so it has methods, which for now you can think of as a function that belongs to an object. Calling a method on a List object returns a new list with a new value. This is how Lists remain useful. There are precisely one zillion List methods. Here are a few. These are beyond the scope of this course so check the link on the slide if you're curious now.

5. How Lists are useful while immutable

Here IS an example for this course. Say we want to add Sindhu to our list of players for a four-player game of Twenty-One. The colon colon operator prepends a new element to the start of an existing List. Since List is immutable, we need to define a new val (newPlayers) since the result is a completely new list with a new value.

6. How Lists are useful while immutable

Or we could define players as a var, then reassign that var the result of the new list produced by "Sindhu" colon colon players. The colon colon operator is pronounced

7. cons (::)

"cons" and it's an operator unique to Lists in Scala. Having something like cons that constructs lists is popular in functional programming languages in general. You'll use cons often. cons PREPENDS. An APPEND operation exists in Scala, but it's rarely used because its not efficient. Advanced topic, link on slide.

8. Nil

Nil is the shorthand way of specifying an empty list. Empty signified in the output here by no elements in the parentheses.

9. Nil

A common way to initialize new lists is to specify the elements with the cons operator separating them, with Nil as the last element. Nil is needed because the cons operator is actually a method that belongs to List objects!

10. Concatenating Lists

Concatenating Lists is also common. In this script, the triple colon operator is used, which is also an operator slash method unique to Lists. Concatenation demonstrates immutability well as this line of code creates a new List called allPlayers. The original Lists playersA and playersB are not mutated. Pause the video to take this behavior in if you need to.

11. Scala nudges us towards immutability

Since Scala likes us to program favoring immutability, you'll likely use Lists more than you use Arrays.

12. Pros and cons of immutability

The same pros and cons for immutable variables apply to immutable collections, too. We are unable to change, add, or remove elements of a list so no inadvertent changes. Our List code is easier to reason about. We have to write fewer tests. The main con, memory due to data copying. The pros usually outweigh the cons in Scala.

13. Let's practice!

Lists are powerful in Scala, and you're getting there too. Go practice!