WEBVTT 00:00.300 --> 00:01.140 Welcome back. 00:01.840 --> 00:07.770 In this section, you're going to learn about maps and map is a collection type, just like an array 00:07.770 --> 00:14.730 and a slice, it stores data in key value pairs, and it allows you to first look up for the values. 00:16.290 --> 00:21.420 In the rest of this lecture, you're going to learn how to create an English to Turkish dictionary program 00:21.420 --> 00:26.520 using maps so you can understand what a map is and why you might want to use it. 00:26.820 --> 00:28.200 All right, let's get started. 00:28.710 --> 00:34.320 Let's say you want to create a dictionary program that can translate the given English words to Turkish 00:34.320 --> 00:35.310 words like SORP. 00:36.390 --> 00:41.890 There will be millions of words in the dictionary, so it should be efficient and very fast to use. 00:42.510 --> 00:44.230 So how can you write this program? 00:45.000 --> 00:48.550 First, let's try to create a dictionary program using slices. 00:49.410 --> 00:55.740 Let's get an English word from the command line like so let's create two separate slices for English 00:55.740 --> 00:56.670 and Turkish words. 00:57.480 --> 01:02.520 Now let's loop over the English slice and search for the given English words like so. 01:03.410 --> 01:07.460 When the program finds a match, I'm going to print the translation like so. 01:13.430 --> 01:20.060 Notice that here a position of an element in the English slice corresponds to an element in the Turkish 01:20.080 --> 01:20.560 slice. 01:21.170 --> 01:25.430 This way I can retrieve the Turkish words using the index of the English word. 01:26.440 --> 01:30.190 Let's also bring the message if the program cannot find the English word. 01:31.700 --> 01:38.850 As expected, it works quite fine, however, there is a problem here, this algorithm is very inefficient. 01:39.170 --> 01:42.780 Let's say there are millions of elements in this system. 01:43.460 --> 01:49.550 So if you use this algorithm, it will need to search for given birth among millions of elements by 01:49.550 --> 01:52.340 comparing each one of them one by one. 01:53.030 --> 01:55.510 This is called an, oh, an algorithm. 01:55.730 --> 02:01.130 It means that the program is going to spend an increasing amount of time depending on the number of 02:01.130 --> 02:01.580 elements. 02:01.760 --> 02:06.190 So what you should do for a better solution, you can use a map instead. 02:06.200 --> 02:06.860 But why? 02:06.960 --> 02:11.570 It's because a map is a hash table and it uses an old one algorithm. 02:11.600 --> 02:18.440 This means that no matter how large the dataset gets, looking up for a single key from a map on average 02:18.440 --> 02:20.120 takes a constant amount of time. 02:20.390 --> 02:22.580 Let me show you what a map looks like. 02:23.000 --> 02:26.750 A map is consisting of keys and corresponding values. 02:27.200 --> 02:30.080 For example, here, English words are the keys. 02:30.440 --> 02:32.390 The Turkish words are the elements. 02:32.930 --> 02:36.810 In a map, you can use a key to reach the corresponding element. 02:37.310 --> 02:39.560 For example, one, the key is good. 02:39.680 --> 02:41.510 You get it in Turkish. 02:42.120 --> 02:43.520 And the key is great. 02:43.550 --> 02:45.460 You get Ourika in Turkish. 02:45.470 --> 02:46.010 And so. 02:47.460 --> 02:55.050 Since a key allows a map to reach a corresponding element so each key in a map should be unique, however, 02:55.230 --> 02:58.050 that can be duplicated elements, and that's OK. 02:58.620 --> 02:59.070 All right. 02:59.250 --> 03:02.560 This time, let's create the dictionary using a map instead. 03:03.000 --> 03:07.260 First of all, let's only keep the query part here by removing the rest. 03:08.040 --> 03:11.670 OK, now let's create our dictionary using a map type like. 03:12.870 --> 03:18.810 As I said, there are keys and elements in a map, so you need to declare the types of the keys and 03:18.810 --> 03:20.370 elements here. 03:20.430 --> 03:25.390 This is the key type and this is the element I saw or this map. 03:25.440 --> 03:32.610 All the key elements should be string values like maps, arrays and slices are key value pairs, too. 03:33.000 --> 03:39.870 However, for arrays and slices, you can retrieve an element only by using an integer index or key. 03:39.990 --> 03:45.390 But unlike arrays and slices, the key type of a map doesn't have to be an integer. 03:45.420 --> 03:53.310 It can be a string load 64 or any other comparable type by comparable type, I mean any type that supports 03:53.310 --> 03:54.680 the equality operator. 03:55.470 --> 03:57.600 Now let's print the zero value of a map. 03:58.540 --> 04:04.000 There's also the number of keys in this map, I can use the land function to do that. 04:07.150 --> 04:13.810 As you can see, the value of a map is nil is because a map is also a hidden data structure, just like 04:13.810 --> 04:14.400 a slice. 04:14.410 --> 04:16.290 So it needs to be initialized. 04:16.450 --> 04:19.890 However, for reading excess, you don't have to initialize it. 04:20.230 --> 04:22.270 That's why you can get its length. 04:22.270 --> 04:25.490 Even though the map is near its length is zero. 04:25.600 --> 04:28.090 So there are no elements in this map value yet. 04:28.360 --> 04:32.530 Let me also show you how you can retrieve an element using a slinky. 04:32.620 --> 04:38.320 OK, now I'm going to retrieve the corresponding element value using key LACHSA here. 04:38.350 --> 04:39.690 The key type is a string. 04:40.090 --> 04:43.180 That's why I can retrieve the element using a string key. 04:43.330 --> 04:45.510 The element type is also a string. 04:45.790 --> 04:48.400 So that's why the map returns the string value. 04:48.580 --> 04:49.780 OK, let's print the. 04:51.890 --> 04:57.800 I have used that verb here because I'm going to print different types of values, you will see them 04:57.800 --> 04:58.220 in a minute. 04:59.030 --> 05:05.540 OK, as you can see as an element, the map returns to zero value for strings, getting a nonexisting 05:05.540 --> 05:06.280 key return. 05:06.290 --> 05:07.460 So zero value here. 05:07.460 --> 05:11.280 It returns an empty string because that is the zero value for strings. 05:11.950 --> 05:14.410 Let me change the element type to an entire. 05:14.990 --> 05:18.490 Now it returns zero because it is the zero value, for instance. 05:18.950 --> 05:25.370 Let me change the element type to bool note returns false because it is the zero value for bulls. 05:25.760 --> 05:30.610 So in summary, you can use different types as the key and element types for a map. 05:31.190 --> 05:34.550 You can even declare a more complex map such as this one. 05:35.270 --> 05:41.240 The key type of this map is a string, whereas its element type is a flawed six to fossilize. 05:41.870 --> 05:47.330 From now on, please don't be surprised when I say value instead of element, they are the same. 05:49.110 --> 05:56.070 As I said before, MapQuest should be comparable if the key is not comparable, then it cannot compare 05:56.070 --> 05:57.960 the given key with the keys in the map. 05:58.140 --> 06:02.080 It also needs comparability for some optimization purposes. 06:02.400 --> 06:06.180 So a map key should always be a comparable time, for example. 06:06.210 --> 06:11.010 This declaration is fine because it uses a comparable key to power. 06:11.160 --> 06:17.850 This declaration is incorrect because it uses a slice as the key to slice, map and function. 06:17.850 --> 06:22.560 Values are not comparable, so you cannot use them as a key type to a map. 06:23.710 --> 06:26.170 Now, let's declare an incorrect map. 06:28.580 --> 06:30.790 This time, the key type is an insult. 06:31.560 --> 06:37.130 Remember, a slice is not a comparable type, so you cannot create a map like so. 06:39.590 --> 06:43.670 Here, the key type is Int and the value type is an int. 06:44.480 --> 06:50.750 So this map declaration is fine because the value type of a map doesn't need to be a comparable type. 06:51.230 --> 06:52.770 Here is another incorrect one. 06:53.360 --> 06:58.750 It's because a map is not a comparable value, so it cannot be a key type to another map. 06:59.450 --> 07:05.280 For example, just like a slice, you cannot compare a map to another map even to itself. 07:05.960 --> 07:09.210 You can only compare it to a new value anyway. 07:09.560 --> 07:14.750 So in summary and map t should be a comparable type when selecting a map. 07:14.820 --> 07:22.010 We don't use a flawed key like this because, as you know, flawed values are not that good for comparability. 07:22.160 --> 07:27.250 So I don't recommend them instead prefer integer or string keys found puzzle. 07:27.260 --> 07:28.340 They are more efficient. 07:28.370 --> 07:31.510 The current map implementation optimizes your code. 07:31.520 --> 07:33.800 Mannus integer or string keys. 07:33.830 --> 07:34.370 All right. 07:34.580 --> 07:39.560 As you can see, you can use a map value for read-only access without initializing it. 07:39.680 --> 07:45.470 If you couldn't read an initialized map, you would always need to check whether it's nil or not before 07:45.470 --> 07:46.050 using it. 07:46.640 --> 07:48.650 Fortunately, this is unnecessary. 07:48.870 --> 07:53.390 Let's add a new translation to the map like so here. 07:53.390 --> 07:58.960 First, I retrieve the value using the up key, then I try to assign it a new value. 07:59.060 --> 08:00.770 Let me add one more word. 08:02.190 --> 08:09.180 Now, there is an error, it says that the map is on initialised, but why is that said behind the scenes? 08:09.180 --> 08:14.760 And MAP is a complex data structure, so you cannot be assigned to a map which doesn't exist yet. 08:14.790 --> 08:17.100 So how can you initialize a map? 08:17.100 --> 08:22.260 Actually is just like a race and slices in the next lecture. 08:22.290 --> 08:26.780 I'm going to show you how to initialize a map and a few other things as well. 08:27.450 --> 08:28.020 So you there?