Welcome back.
So let's look at one more collection that is going to be the set.
So that is a type of collection that eliminates duplicate data.
This collection is unordered, which means the elements are not sorted.
The midtable class is set off while the mutable types are mutable set off.
And then there's also a hash set off.
So let's look at an example here.
So first, I'm going to create this fruit's example, which will be a set of, for example, orange.
Then you have Apple Grape.
And finally, another Apple while we're at it.
So now let's go ahead and print the size of the fruits set.
So here?
Print size, like so let's run it.
And you will see that at this press three.
So what's up here?
Well, that is because our orange or apple and our grape are the only unique items inside of the set.
It ignores this apple.
Now let's add mango in here, for example, as well.
And I'm going to put that right there.
Mango.
Very tasty.
So we have orange, apple, mango grape and another apple.
Now you could go as far as to add another orange.
And what you will see is that you still get an while.
This time you get four because we had the mango, but the apple and orange, the duplicates, so to
speak, they are ignored.
They are not taken into consideration.
Now the cool thing is we can sort this list so we can, of course, also display fruits.
So let's run this real quick.
We'll see orange apple mango grape.
So it ignored all of the additional details, and now we can also sort it by using two sorted set.
So that's a very cool feature.
So let's do that and you see Apple Grape, Mango Orange, so it's sorted it alphabetically.
Now, the problem with this set is that it's not mutable.
So let's go ahead and create a mutable list.
So I'm going to call this one new fruits, and it will be a fruits to mutable list.
So now this time we are converting our fruits into a mutable list of fruits.
And now we can go ahead and use new fruits in order to add items to it.
So, for example, a watermelon would be a very good addition to this.
And then let's add another beautiful fruit that would be a pear.
Like, so OK.
And then let's print those.
So print new fruits and let's see what we're going to get.
And we see we have Apple Grape, Mango Orange.
This is the first entry, this is this print statement on let me get rid of it in order to make this
look a little smoother.
We see orange, apple, mango, grape, watermelon, pear.
OK, so it added those entries for us.
We want to access a particular fruit in your list.
You can go ahead and just use the element pat.
And then the number that you want to have.
So new fruits element at four.
Let's see what's going to happen there and we see we get the watermelon because the watermelon at this
point is at the index four, which means it's the fifth fruit because we have orange, apple, mango
and grape.
And then we have the watermelon.
And that's index four because oranges at zero, apple at one mango, two grape at three watermelon four
and pear at five.
OK, now let's look at maps, OK, and we're not going to look at Google Maps or so, but we're going
to create a map of where a map list of kind of collection.
So Map is a type of collection that holds data in the form of a key value pair, and the map keys are
unique and hold only one value for each key.
So let's look at that.
So we have days of the week, for example, and this time we're going to use a map of here.
So this will be a map of one to Monday, for example.
That's how you can map.
Very easy way.
Pretty cool.
And here you need to have the empty spaces.
So one to Monday, then two to four example Tuesday at three to Wednesday.
So.
I have now connected the numbers to the dates, and that means that this key, this is the key is connected
to the value of Wednesday.
The key one is connected to the value of Monday.
So this key can only be existing once each key only exists once.
So this is a map.
But in other languages, this is also called a hash map or a dictionary.
So depending on which programming language you're looking at, it just has different names.
And then we can now go ahead and, for example, access a particular item or print days of week at position
two, for example, would give us Tuesday.
OK, so here we're not talking about the index, but the actual key.
OK gets the key value and not the index.
So let's run this and we will get the Tuesday entry because it is connected to the key of two if you
want to loop through your items inside of a map.
You can just go ahead and use for loop again, saying key in days of week dot keys.
So now we're going through the individual keys and let's print.
The key is to and then.
The days of week dot.
Well, in this case.
The square brackets key and what this will give us, it will tell us what the key is and it will tell
us the item that it is connected to.
So this is the value that it's connected to.
And here I'm using numbers to strings, but this could also be strings to, for example, objects.
So you're not limited here.
It's what gives you a lot of freedom here.
So let's run this and see.
One is to Monday to is to choose two entries to wed.
So just a quick example of using our fruit data again.
So here, data class fruit, which contains the value of name, which will be a string and then price,
which will be a double.
OK, so we have this beautiful fruit now, this fruit class, and now we could go ahead and say fruits.
It's going to be a map of this will be fruits map.
Let's call it like this.
So that is unique map of.
And then I connect, for example, one to my fruit, which is going to be grape and has the price of
two point five.
And then we have to assigned to fruit that will be mine actually here.
You could even go ahead and use not grape, but favorite fruit, something like this favorite.
And this would be OK.
And here the fruit would be.
Apple, even though I love apples and it has the price of.
One Euro 1.0 euro in order to press to be precise.
OK, so that will be our map of.
And me find a good point to set the brake.
So you see, we have this fruits map of where our key this time is a string and the value is an object.
In our simple example here, our key was a number and the value was a string.
So you can make whatever connections you want to have here, which is why maps are so powerful.
Now, if you want to make changes, you need to make it a mutable list.
So here I can just go ahead and change that to all new days of week or new day of week.
And that will be days of the week.
OK.
And I'm going to make it the mutable map, so to mute a mob and now I can use the new days of the week
to assign, for example, at the position four I can add.
First day.
And then at position five, I could add.
Friday.
And so forth.
OK.
And you can now sort her map by using the two sorted map method.
So here, Brent new days of week dot to sorted map out this well sort of map for us.
Let's run this real quick and we can see we have the sorted map and actually we have another print statements
somewhere, which makes this a little more difficult to read.
I believe this one.
So let's get rid of this and run it again, and we can see now it's sorted.
It's Monday, Tuesday, Wednesday, Thursday, Friday, and it's sorted by its key.
So just like other collections, MEPs also have a bunch of different methods that you can play around
with.
So I would really recommend that you just go ahead and use to dot operator and just give yourself some
hints.
OK, so what you can do here, for example, it doesn't have an ad method, but it has other methods.
So here, if you press the dot key, you can see there are a bunch of methods.
In my case, the idea seems to be broken, so I'm going to fix that.
OK, so that's it for this video.
See you in the next one.