1 00:00:00,250 --> 00:00:03,060 Slices are good for storing collections of data, but 2 00:00:03,060 --> 00:00:07,790 the only way you can get data back out of a slice is by using a numeric index. 3 00:00:07,790 --> 00:00:11,630 Sometimes it's useful to be able to use other values as keys that let you look up 4 00:00:11,630 --> 00:00:13,290 items in a collection. 5 00:00:13,290 --> 00:00:16,492 Many programing languages support collections like this. 6 00:00:16,492 --> 00:00:17,220 In C# and 7 00:00:17,220 --> 00:00:22,890 Python they're called dictionaries, in Ruby it's a hash, in Java it's a hash map. 8 00:00:22,890 --> 00:00:27,200 Gojes seizes the term map to refer to this sort of key value list. 9 00:00:27,200 --> 00:00:31,430 The idea is that a map maps a set of keys to corresponding values. 10 00:00:31,430 --> 00:00:35,760 You can use any type you want as a key as long as the keys are all the same type. 11 00:00:35,760 --> 00:00:37,870 If you know the key a value is stored under, 12 00:00:37,870 --> 00:00:40,410 you can use the key to retrieve that value. 13 00:00:40,410 --> 00:00:44,420 Let's create a map that stores the names and ages of some students. 14 00:00:44,420 --> 00:00:48,710 So we're gonna specify that it should create a new map, and 15 00:00:48,710 --> 00:00:52,910 here we specify the type that should be used for the keys. 16 00:00:52,910 --> 00:00:57,812 Since we're using names as keys, we're gonna use a type of string. 17 00:00:57,812 --> 00:01:01,715 And since the values will be ages, we'll go ahead and 18 00:01:01,715 --> 00:01:04,161 use the value type of float 64. 19 00:01:04,161 --> 00:01:07,640 We'll specify that the map should be empty when it starts. 20 00:01:07,640 --> 00:01:10,844 And we'll assign the resulting map to the ages variable. 21 00:01:13,854 --> 00:01:18,814 Now, let's assign some values to keys within the map, so this is just like 22 00:01:18,814 --> 00:01:24,014 the syntax for assigning to an array index except that instead of being limited 23 00:01:24,014 --> 00:01:29,490 to numeric indexes, we can use any value of the type we've specified for the key. 24 00:01:29,490 --> 00:01:31,830 So we can use any string we want. 25 00:01:31,830 --> 00:01:37,120 So we'll create a student with the name of Alice and we'll say that her age is 12. 26 00:01:37,120 --> 00:01:40,330 And then we'll assign a second key, Bob. 27 00:01:42,020 --> 00:01:44,300 And we'll say that his age is nine. 28 00:01:44,300 --> 00:01:46,350 Then let's print out the whole map. 29 00:01:46,350 --> 00:01:50,930 So format, printline, ages. 30 00:01:50,930 --> 00:01:52,170 Okay let's save that. 31 00:01:52,170 --> 00:01:55,125 Try running it. 32 00:01:55,125 --> 00:02:00,016 And we see that the result is a map under the Alice key we have an age of 12 and 33 00:02:00,016 --> 00:02:02,438 under the Bob key we have an age of 9. 34 00:02:02,438 --> 00:02:07,300 The syntax to retrieve an individual value is just like a razor slices two except you 35 00:02:07,300 --> 00:02:10,240 use a key in place of the numeric index again. 36 00:02:10,240 --> 00:02:18,256 So we can say format Println And 37 00:02:18,256 --> 00:02:25,934 let's print the age of Alice, And the age of Bob. 38 00:02:30,517 --> 00:02:35,370 Save that, rerun it, and it prints out the values for those two keys. 39 00:02:36,430 --> 00:02:38,312 Similar to arrays of slices, 40 00:02:38,312 --> 00:02:42,380 we can use a map literal to pre-populate a map with values. 41 00:02:42,380 --> 00:02:45,180 We just type them here within the curly braces. 42 00:02:45,180 --> 00:02:51,570 So we can say, under the key of Alice, there should be an age of 12. 43 00:02:51,570 --> 00:02:52,800 And under the key of Bob. 44 00:02:53,840 --> 00:02:56,040 There should be an age of nine. 45 00:02:56,040 --> 00:03:00,690 Just separate the key from the value with a colon, and 46 00:03:00,690 --> 00:03:04,080 separate multiple key value pairs with commas. 47 00:03:04,080 --> 00:03:05,010 Let's try running this. 48 00:03:07,250 --> 00:03:09,350 And there's our populated map. 49 00:03:09,350 --> 00:03:12,520 And, as with arrays of slices, we can use the for 50 00:03:12,520 --> 00:03:16,660 range loop to loop over each item that a map contains. 51 00:03:16,660 --> 00:03:19,720 Each key will be assigned to the first variable you provide. 52 00:03:19,720 --> 00:03:22,400 And each value will be assigned to the second variable. 53 00:03:22,400 --> 00:03:25,810 So if we were to run this for range loop, 54 00:03:25,810 --> 00:03:31,030 it would print out a list of each of the names and the corresponding ages. 55 00:03:31,030 --> 00:03:34,210 If you don't need to work with the keys 56 00:03:34,210 --> 00:03:37,480 from a map you can ignore them by using the blank identifier. 57 00:03:37,480 --> 00:03:42,120 So for example we can print just the ages by using the blank 58 00:03:42,120 --> 00:03:43,770 identifier in place of the keys. 59 00:03:43,770 --> 00:03:47,630 If we were to try running this, it'll print out just the ages. 60 00:03:47,630 --> 00:03:53,120 Or if we want to ignore the values, and use just the keys, 61 00:03:53,120 --> 00:03:58,740 we can just eliminate the second variable that holds the values. 62 00:03:58,740 --> 00:04:01,680 So we can print just the names with this code. 63 00:04:02,840 --> 00:04:06,280 Try running that and we get a list of just the names.