List vs ListBuffer6:27 with andi mitre
A list is another data structure which maintains the order of the elements, allows for duplication and is immutable. Similarly to the ArrayBuffer, the ListBuffer is the mutable form of a list.
List -- A list is an immutable data structure which can contain duplicates and maintains the order if its elements.
ListBuffer -- A ListBuffer is the mutable implementation of a list.
In this video, we learn about lists and list buffers. 0:00 A list is another data structure which maintains the order of the elements, 0:04 allow for duplication and is immutable. 0:08 Similarly to the array buffer, the list buffer is the mutable form of a list, 0:11 let's dive in and implement some lists. 0:16 There are a few ways to create a list in Scala. 0:19 However, the simplest form is by calling the list keyword. 0:22 Let's jump back into the code and implement some lists. 0:26 Back into our collections package in our super hero object, we'll clean up 0:30 everything associated to arrays and array buffers, just for simplicity. 0:34 Awesome, this creates an immutable list of type string. 0:56 We can additionally create a list in Scala by using the cons operator noted by colon, 0:59 colon and the keyword nil at the tail end of the list. 1:06 By having cons with nil at the end of the statement, 1:18 creates a list that holds the numbers 1,2,3. 1:21 Therefore, numbers is a list of integers containing one, two and three. 1:25 Lists are very fast at retrieving the first and the last element in the list. 1:30 To get the first element, we call the head method. 1:36 And for any element besides the first, we can call the tell method. 1:38 This is probably different from what you may expect in other languages. 1:43 Let's compile and rerun our app. 1:53 As expected the immutable heroes that head returns the first element in our list, 2:00 Wonder Woman. 2:05 Additionally, the immutable heroes that tale returns everything but 2:06 the first element. 2:10 Therefore, a list including Thor, Superman, and Batman. 2:12 Another neat function which applies to many collections 2:17 as well as lists is MK string. 2:20 MK string or Make String can be used to print the elements of a collection as well 2:23 as it allows you to add a prefix or a suffix to any element when it is printed. 2:28 In this example, we provide a prefix and our suffix to our immutable heroes list. 2:54 For each element, we use a comma separator. 2:59 Let's recompile our app and print out our results. 3:02 Awesome, similar to how arrays have a mutable array buffer, 3:12 lists also have a mutable list buffer. 3:16 To create a list buffer, the mutable form of a list, 3:19 we need to import it from a Scala collections package. 3:23 Let’s do so at the top of our file. 3:27 Importing the package provides us with the flexibility to add, remove or 3:36 even update our superheroes over time. 3:41 We can now append to the list buffer by using the append method or 4:01 simply providing the symbol plus equals. 4:04 In order to prepend, we would use the plus equals colon symbol. 4:07 Let's check these out. 4:12 Let's recompile and run our app. 4:31 Awesome, we prepended Captain America to our list, and 4:36 we also append Raven to the end of the list. 4:40 In order to access an element of the list, we use the parentheses notation. 4:44 And we provide the index based value. 4:48 Because list buffers are mutable, we can also update a specific element. 4:58 Let's remove Thor and replace him with Iron Man. 5:03 Let's comment out on our print line statements and check out our results. 5:12 We can remove elements from a list, using the minus equals symbol, or 5:31 calling the remove method. 5:35 Additionally, we can remove elements by value or even by index. 5:37 Let's start by removing an element by value. 5:42 The tarsal remove an element in mutable heroes by index. 5:51 Awesome. 6:07 We get back Wonder Woman, Iron Man, Superman, and Raven which means 6:08 that we're able to successfully remove Batman and also Captain America. 6:13 Great, we see how list and list buffers work. 6:18 In the next video, we will dive into maps and tuples and 6:21 see how they're useful to our everyday code. 6:25
You need to sign up for Treehouse in order to download course files.Sign up