Maps and Tuples9:01 with andi mitre
In this video we will learn how to implement maps and tuples in Scala. Maps or hash tables are collections of key value pairs. Tuples are an immutable sequence of values and are used for aggregating values.
Hi, in this video we'll learn how to implement maps and tuples in Scala. 0:00 Maps or hash tables are collections of key value pairs. 0:05 Tuples, on the other hand, are an immutable sequence of values. 0:10 They are also quite useful for aggregating values. 0:13 Let's get a better idea on these topics by implementing some examples. 0:17 Similar to previous videos, Scala collections can be mutable or immutable. 0:22 By default, when we construct the map in Scala it'll be immutable. 0:27 Let's see how that works syntactically. 0:32 First, let's clean up our superheros object and 0:35 remove anything associated to lists or list buffers. 0:38 Awesome. 1:14 We have created a map of type string and 1:14 integer with the superhero name and ranking. 1:17 Next, let's see how we can access values in our map. 1:21 Similar to Java, except without using the get keyword, 1:34 we look up values in a map using parenthesis and passing in the key. 1:37 And in this case we'll return a ranking of ten. 1:42 You may have noticed that the key is case sensitive, and 1:45 it is possible that the key we are looking for is either misspelled or doesn't exist. 1:49 For instance. 1:54 This will throw a no such element exception because the key is now found and 2:03 it'll crash our program. 2:07 Let's recompile and run our app. 2:10 As expected, we get a no such element exception that crashes our app. 2:13 It is considered good practice to use a method called getOrElse, 2:18 which will return the value if the map contains the key. 2:22 Otherwise, it will return a default value. 2:26 Let's take a look. 2:28 In this example, the code will either return the value of the key, 2:39 ironMan, or zero if it is not found. 2:44 But now our program is safe from the key not found exception and won't crash. 2:46 Additionally, we can retrieve out the keys and 2:51 values over our map using .keys and .values respectively. 2:56 Awesome, as we can see, the .keys method returns a set which is a collection that 3:13 we'll discuss in the next video. 3:18 Meanwhile, the .values returns a map-like collection with the values of our keys. 3:20 Great, we learned how to create HashMaps, let's take a look at how to traverse them. 3:26 The following loop construct allows us to iterate 3:31 over all the key value pairs in the map. 3:34 Lets print out the key value pairs of our Avengers map. 3:37 Great, if we're only interested in the values, 3:58 we can loop through avengers.values. 4:01 Let's recompile and run our app. 4:14 Awesome, so far we've only implemented the mutable map. 4:17 Let's construct a mutable map and see how we can use it to update values and 4:22 extend our map. 4:26 Let's import the scala.collections package at the top of our file and 4:28 create a mutable map. 4:34 Awesome, this creates a mutable map of type string and integer. 5:13 Now we can update the contents of the map. 5:18 Let's up Iron Man's ranking as Tony Stark's new invention makes him much, 5:20 much stronger. 5:25 Now we can also add a key value pair to a map using the += method 5:35 Additionally, to remove an element we can use the -= method and 5:49 pass the key of the element we would like to remove. 5:53 Let's take a look. 5:57 Great, let's comment out some of our previous print line statements and 6:04 recompile our app. 6:08 As expected, we've changed the ranking on Iron Man, we've added Hawkeye, and 6:32 we also removed Captain America from our map. 6:37 Awesome, we now have a really good understanding of how maps work. 6:41 In Scala, 6:45 tuples are very easily created by providing values enclosed in parentheses. 6:46 Let's switch back into our code and create some tuples. 6:51 In this example, we've created a tuple three instance with the types string, 7:15 string, and char. 7:19 Tuples in Scala are actually classes that contain a collection of elements. 7:21 Scala only supports up to tuple 22. 7:26 Which means that we can only have up to 22 elements in a tuple. 7:29 Unlike some of the other collections we saw, tuples are not zero index space, but 7:34 rather they start at one. 7:38 Accessing the first element in a tuple can be done 7:40 with an underscore followed by the index value. 7:43 Let's take a look. 7:46 Let's recompile and run our app. 8:11 Awesome, we were able to retrieve Hawkeye's name and also his birthplace. 8:18 To loop through the elements in a tuple, 8:23 we have to use a method called product iterator. 8:25 Great, let's recompile our app and check out our results. 8:39 As expected, we've printed out the name, birthplace and gender of Hawkeye. 8:44 Awesome, we learned what maps and tuples are and how to perform operations on them. 8:50 In the next video, we will learn about another data structure called sets. 8:55 See you there. 9:00
You need to sign up for Treehouse in order to download course files.Sign up