In this lecture, we're going to be talking about new data structures in ethics, which are maps and
sets, those new data structures can be very useful and helpful in many real world situations.
At first I'm going to introduce you map actually maps to our data as key value pairs.
And one of the most important things regarding maps is that we're able to store any data types as keys
and values.
All right.
Let's demonstrate how to create maps, syntactically create variable and college map data.
Actually, we need to create maps using construct annotation.
For that, we have to use new operator and that the keyword map with capital letter.
Let's run it in council and see what looks like right map data.
Reload, and here it is.
So the first thing that we see here is Sise, which actually is a prop. and it gives us the size of
a map.
I mean, that it indicates the number of key value pairs.
It's actually like the length property of an array.
If we drop down, underscore, underscore, Proteau, underscore, underscore, will find here the methods
which we are able to use with maps, one of the methods that you see here is set.
So at first I'm going to use it actually set method allows us to insert data into a map.
So let's right map data dot set and then write inside parentheses as a key name and then as value John.
Reload and see that item is added in this case, both key and value is represented as drinks, but instead
of drinks, we're able to use numbers, boolean values, arrays, objects and even functions.
Let's add as a key boolean value right.
Map data that set right here through.
And then the value, yes.
Also, let's create an object, right, cost, constant object.
And then added as key.
Right, again, map data that set object.
And then as a value right person.
Reload the page and you said that we have here our newly added key value pairs.
All right, in order to get the size of map, we just need to use size property using dot notation,
let's run it in council duplicate and write your dot size.
So here we have three, because MAP actually contains three items, so using that method is one of the
ways to add items to map.
But we can initialize map with data by passing an array, which itself will include other arrays with
key value pairs.
Let's see how it works.
Open square brackets that create array with key turn and with value again.
Right turn then create another array.
Right is married.
And the value false.
Reload and you see, we have your newly added items.
All right, that's actually how we add items to map and now let's see how we can get access on them
for that.
There is one of the methods which is called Get.
So in order to get item, for example, name, we have to.
Right.
Cancel that log map data that get.
And then the name.
Reload and we have value of nine key, which is John, OK, in order to check if there exists the specified
key, we can use one of the methods which is called has.
So if the key exists, it returns true value, otherwise it returns false.
Let's check in from updater contains object key.
So write that log.
Map data that has.
And then insert here, object, reload, and we have to because map contains the key keep all right,
besides the hazmat that we had delete, which actually allows us to delete from a single key value pairs.
So, for example, if we write map data, dot delete.
And indicate here one of the keys, for example, said.
And as a result, you will see that with its value is removed from that, OK, in order to delete the
entire data from up, we can use Carlier method, which actually will make our map empty.
So that's right.
Mark Dot Carlier.
Then reload and I see that map is empty.
All right, let's delete clear method.
And if we check on the score on the score, Proteau underscore and the score we will find here for each
array helper method, and I would like to note that this is the only array, Harpur, which we are able
to use with maps.
So that's the latest council that logs and use, for example, with map data.
Right.
Map DataDot forage as a first parameter.
We're going to place your value because at first each method will return values of keys.
And then as the second parameter that insert here key.
Run in council.
Value add key.
Reload and we have here keys and values there was not that we were expecting, OK, besides for a chopper
we can use for of statement with maps.
So that's comments for each method out and then.
Right for.
Suppose that we want to get kids from app, so let's create let QI then use the keyboard of.
In order to get all the kids from that, we have to use one of the methods of KNAB, which is called
keys.
So right up DataDot Keys.
And that Iranian castle.
Right here, keep.
Let's reload the page and.
You say that we have here are the keys from up in the same way we can get all the values, and for that
we have to use other method, which is called values.
So let's change your keys into a values.
Also right value instead of key.
Reload.
And now we have the values.
All right, if we want to get simultaneously keys and values, we can use another method which is at
risk.
So let's change values into entries.
And also right here, instead of value array, because entries method actually returns key value pairs
as a race.
So let's reload and you see that we have a race which consists of key value pairs.
If we don't want to get them as a race that simply we can distract you, then.
So that's change arriving to key value right inside square brackets and also round them in Castle.
Reload and we have now keys and values represented without a race.
All right, it was all about maps.
Let's go ahead and talk about another new data structure in Essex, which is set actually set holds
values without keys.
And mostly it's used in real world when we want to store all the unique values.
Let's see what it means.
I'm going to comment this code out.
In order to create said, we have to use again construct a notation, let's create variable and call
it set data.
Then use new operator and right set.
Run it in council in order to see what it actually looks like, right?
Log.
Set Theta.
So, like Matt said, has size property as well, and if we drop down on the score on the score prior
to underscore on the score, we will find here some methods that we have already seen with the map.
But if you look at them carefully, you will notice that actually some of them are missing because they
are not used with sets.
All right, let's see how we can add items into that, we have two ways for doing that.
The first one is to use add method.
So if we write set data, dots add.
With the value neek.
That without.
We will have here, Nick.
Like it was in case of map, we can add as values, any data type, and you can test it on your own.
The second method to add items is to place your inside parentheses array and then add as many items
as we want.
So that answer to your.
John, that.
San.
Then, for example, false value.
Also, place your object with property first name.
Bob.
And to the end, let's add here, John, once again and see what happens, reload.
So we have here all the items that we have just added, but you notice that John is added here only
once, that's because generally set stores only unique values.
So if we check the size of that.
Then we will get five.
So actually, we have added six items, but because of that, we have repeated, John said, started
only once, and that's why we have here five instead of six.
All right, in case of sets we can use has the lead clear methods also right for a chopper, but I'm
not going to waste time on that because they work in the exact same way as it was in case of maps.
The last thing that I'm going to do is to use four of statement because it gives us a bit different
results.
So let's create for that let's value of such data.
That values.
That Romney town council.
So if we revote, we will get values.
The same will happen if we use your case method instead of values.
And it happens because they actually said, doesn't have Keith.
But if we use Andris method.
Instead of kiss.
Then we will get a raise and you see that each of these arrays contain duplicated values.
Honestly, the cases of keys and interest methods are kind of weird, but that's the way how they work.
All right, this was all about new data structures in year six maps and sets, I think both of them
are very flexible and powerful data types, especially a map, and they can be used in many cases instead
of objects and a rise in real world.
Also, the key point is that they work in very simple white.
OK, let's move on.