Welcome back.
In this section, you're going to learn about maps and map is a collection type, just like an array
and a slice, it stores data in key value pairs, and it allows you to first look up for the values.
In the rest of this lecture, you're going to learn how to create an English to Turkish dictionary program
using maps so you can understand what a map is and why you might want to use it.
All right, let's get started.
Let's say you want to create a dictionary program that can translate the given English words to Turkish
words like SORP.
There will be millions of words in the dictionary, so it should be efficient and very fast to use.
So how can you write this program?
First, let's try to create a dictionary program using slices.
Let's get an English word from the command line like so let's create two separate slices for English
and Turkish words.
Now let's loop over the English slice and search for the given English words like so.
When the program finds a match, I'm going to print the translation like so.
Notice that here a position of an element in the English slice corresponds to an element in the Turkish
slice.
This way I can retrieve the Turkish words using the index of the English word.
Let's also bring the message if the program cannot find the English word.
As expected, it works quite fine, however, there is a problem here, this algorithm is very inefficient.
Let's say there are millions of elements in this system.
So if you use this algorithm, it will need to search for given birth among millions of elements by
comparing each one of them one by one.
This is called an, oh, an algorithm.
It means that the program is going to spend an increasing amount of time depending on the number of
elements.
So what you should do for a better solution, you can use a map instead.
But why?
It's because a map is a hash table and it uses an old one algorithm.
This means that no matter how large the dataset gets, looking up for a single key from a map on average
takes a constant amount of time.
Let me show you what a map looks like.
A map is consisting of keys and corresponding values.
For example, here, English words are the keys.
The Turkish words are the elements.
In a map, you can use a key to reach the corresponding element.
For example, one, the key is good.
You get it in Turkish.
And the key is great.
You get Ourika in Turkish.
And so.
Since a key allows a map to reach a corresponding element so each key in a map should be unique, however,
that can be duplicated elements, and that's OK.
All right.
This time, let's create the dictionary using a map instead.
First of all, let's only keep the query part here by removing the rest.
OK, now let's create our dictionary using a map type like.
As I said, there are keys and elements in a map, so you need to declare the types of the keys and
elements here.
This is the key type and this is the element I saw or this map.
All the key elements should be string values like maps, arrays and slices are key value pairs, too.
However, for arrays and slices, you can retrieve an element only by using an integer index or key.
But unlike arrays and slices, the key type of a map doesn't have to be an integer.
It can be a string load 64 or any other comparable type by comparable type, I mean any type that supports
the equality operator.
Now let's print the zero value of a map.
There's also the number of keys in this map, I can use the land function to do that.
As you can see, the value of a map is nil is because a map is also a hidden data structure, just like
a slice.
So it needs to be initialized.
However, for reading excess, you don't have to initialize it.
That's why you can get its length.
Even though the map is near its length is zero.
So there are no elements in this map value yet.
Let me also show you how you can retrieve an element using a slinky.
OK, now I'm going to retrieve the corresponding element value using key LACHSA here.
The key type is a string.
That's why I can retrieve the element using a string key.
The element type is also a string.
So that's why the map returns the string value.
OK, let's print the.
I have used that verb here because I'm going to print different types of values, you will see them
in a minute.
OK, as you can see as an element, the map returns to zero value for strings, getting a nonexisting
key return.
So zero value here.
It returns an empty string because that is the zero value for strings.
Let me change the element type to an entire.
Now it returns zero because it is the zero value, for instance.
Let me change the element type to bool note returns false because it is the zero value for bulls.
So in summary, you can use different types as the key and element types for a map.
You can even declare a more complex map such as this one.
The key type of this map is a string, whereas its element type is a flawed six to fossilize.
From now on, please don't be surprised when I say value instead of element, they are the same.
As I said before, MapQuest should be comparable if the key is not comparable, then it cannot compare
the given key with the keys in the map.
It also needs comparability for some optimization purposes.
So a map key should always be a comparable time, for example.
This declaration is fine because it uses a comparable key to power.
This declaration is incorrect because it uses a slice as the key to slice, map and function.
Values are not comparable, so you cannot use them as a key type to a map.
Now, let's declare an incorrect map.
This time, the key type is an insult.
Remember, a slice is not a comparable type, so you cannot create a map like so.
Here, the key type is Int and the value type is an int.
So this map declaration is fine because the value type of a map doesn't need to be a comparable type.
Here is another incorrect one.
It's because a map is not a comparable value, so it cannot be a key type to another map.
For example, just like a slice, you cannot compare a map to another map even to itself.
You can only compare it to a new value anyway.
So in summary and map t should be a comparable type when selecting a map.
We don't use a flawed key like this because, as you know, flawed values are not that good for comparability.
So I don't recommend them instead prefer integer or string keys found puzzle.
They are more efficient.
The current map implementation optimizes your code.
Mannus integer or string keys.
All right.
As you can see, you can use a map value for read-only access without initializing it.
If you couldn't read an initialized map, you would always need to check whether it's nil or not before
using it.
Fortunately, this is unnecessary.
Let's add a new translation to the map like so here.
First, I retrieve the value using the up key, then I try to assign it a new value.
Let me add one more word.
Now, there is an error, it says that the map is on initialised, but why is that said behind the scenes?
And MAP is a complex data structure, so you cannot be assigned to a map which doesn't exist yet.
So how can you initialize a map?
Actually is just like a race and slices in the next lecture.
I'm going to show you how to initialize a map and a few other things as well.
So you there?