00:00Next time, we will talk about how we iterate on the hash maps. We will use a set. A set
00:09is another data structure which we will study later in detail. But now we think that in the hash map
00:14there is one method. This method is called keyset.
00:18Hash Map, what does it do? Hash Map, all the keys have all the keys, they make all the set,
00:23they make a collection, and return it. Like if I have a Hash Map, which has many nodes. Let's say,
00:31this is my India node, which is the population store 100. This is my China node, which is the population
00:37150. This is Nepal node, which is the population 5k equal. This is the US, which is the population 50k
00:44equal. So what will it do? Hash Map,
00:47But if I call this method, then I will make another new collection and I will name this set. In
00:55this set, all the keys will be stored. Like India will be stored, China will be stored, Nepal will be
01:05stored, US will be stored. So all the keys will be stored. Then what will I do?
01:14in the hash map. And what is the process of writing it? We write hm.hashmap.keyset and the keyset
01:21return and we store all the keys in a set. So in the set, we can also write a type
01:26here. If my keys type is integer, then we will put integer. If my keys type is string, then I
01:32will put string here and I have given it to the name of the keys. So all the keys will
01:36store in this set.
01:38In the main function, I have made a map in which I have stored many data. Every time I have
01:44stored key and value pairs.
01:47Now I have to iterate on this map, so I will create a set of strings. I will say the
01:53set of strings here, because my keys are string type. It may be the keys integer type, it may be
01:58the boolean type, boolean we store two keys in boolean, because it can be true and false. So the keys
02:04type of type we can write here, then I will write keys is equal to hash map dot keyset.
02:12In the key set we mistake many students in the key set. We take this S to small. So this
02:17is not lower case, there is S capital in the set. So all the keys will return me to the
02:23set and I can print these keys too. I simply write keys. So for this map, all the keys come
02:32from China, US, Nepal, India, Indonesia and these keys are no order. It doesn't mean that they are sorted order
02:39or alphabetical order. They can show us in any random order.
02:45You thought you would have said that you would iterate the loop, so we did not just print the keys.
02:50So what will I do? I am going to run a loop on the increase. And for each key, I
02:56will remove the value from hash map. I have a special type of loop which we have also added in
03:01the previous classes. We call it for each loop. For each loop is used to iterate on an array or
03:08some other data structure. So I will write for each.
03:13After clicking this, the syntax remains something that we can write for, and we can write what we want to
03:19do with the iterator, plus the parent type. So, what will happen in each set? A string key will be.
03:26This string will give me a name, a single key.
03:31I have key which is my k to equal and which data structure I have put a loop on the
03:35keys. So for each loop we have already read in java. You can also search it on google search, if
03:41you want to learn more about it or not, then we can see our entire classes. So what will I
03:45do? Now I will print key equals
03:51Here I will print k value here. Now the key will be removed from the key and the key will
03:56be stored. So first I will print k value plus if I want to print value here I will write
04:03it here in a good way. Value equals hash map dot get key
04:19So what will happen first? My key will print my key. After that, we will take value to the key.
04:26Then we will print it. We will do save and run. My key is China, which value is 150. US,
04:38which value is 50. Nepal, which value is 5.
04:42Key is India, which value is 100. Key is Indonesia, which value is 6 is equal. So, in this way,
04:49we put a loop on a hash map. Here, I have not put a loop on a hash map. Here,
04:54I have put a loop on a hash map. Here, I have put a loop on a key set. Now,
04:57we also iterate on a hash map. Here, I have used sets. I mean, I have put a value on
05:06a key set. Besides, there is something called entry set.
05:10in a hash map. If I call hash map dot entry set, then there is a set of keys, entry
05:17set call, and all pairs will come. So we don't need to call each other. But this is also an
05:23efficient technique. If you want entry set, you can also use entry set. It will do the same work. Time
05:28complexity will be enough. Now I can perform many operations to iterate. If I want to know that which key
05:35value is the biggest or which value is the biggest, then internally
05:43If max is less than my current key k. In this case, we can do many logical things when we
05:51iterate on maps.