• 2 days ago
Transcript
00:00Now let's talk about hash map in Java. For use of hash map, we have to import java.util or
00:09.star or not, so we can import directly hash map in Java. For create a hash map, we have to
00:16write hash map and then put angular brackets. Now we have to tell our key type in the angle. So
00:25if I have country name
00:27And for each country, we have to store the population of each country. So, the country will be a string
00:32type. And the population of each country will be a string type. So, I will write here string. And after
00:39that, I will write integer. So, in this way, I will create a hash map. I can give it a
00:45name. HM is equal to new hash map. So, in this way, we will create a hash map.
00:56The key type will be a string and the value type will be a integer. On the hash map, the
01:01operations we have already discussed are in constant time. So, when we use the operation, the name is put. What
01:09do we do with the key value? We will pass the value of the value or the value of the
01:14data. And within the hash map, our data will be put. Let's say I have made a hash map in
01:22which I have many data exists.
01:25I have a value for India, here I have a value for 100, I have a value for US, 50,
01:33and I have a value for China, 150. If I write put India 120,
01:43What do I have done? I have tried to add my data for India. So the hash map already exists
01:51in India. So what do I have done? I will remove this old data and add this node to this
01:59node. So in the hash map, which data is key?
02:03If it exists before, because it can not become duplicate, so if it exists before, then it will be set
02:09in a new value. But if it exists before, then what will happen in this case? If I do put
02:17button 1, then in this case, a new node will be stored in which button will be stored, meaning my
02:24key is also my value.
02:26So my put function works in this way. For this function, we will write code for this function. What I
02:31will do is create a classroom class and create my main function. First of all, I am going to import
02:36my package. Now I will create a hash map. First of all, I will write hash map. Then I will
02:44tell my key type, which is string. Then my value type, which is integer. I will name it HM.
02:51You can also give a map or whatever we want to give them. We will write is equal to new
02:56hash map. So, in this way, my memory has only been initialized in my memory. Now, to insert data into
03:04it, if I have to insert elements, then I will write hm.put. First, I am going to input value
03:15in India. We will put value in India. We will put equal to 1500. Then, I will write hm.put
03:24.china.
03:25We add value for China, 150. Then I will write HM.put. US, US value, 50k equal.
03:34And last, for printing, I simply can write system.out.println which is HashMap. Now, how does HashMap work internally?
03:43We will study it in detail, when we talk about implementation. But now, I have to assume how does HashMap
03:50work internally? How can it perform so fast operations? That is what Java has already done for me. What do
03:57I have to do with my use cases? I have to implement HashMaps and HashSets. Let's save and run.
04:09I have printed my hash map, which shows my app in Java. I have China, which is the value store
04:17for 150. I have US, which is the value store for 50. I have India, which is the value store
04:24for 50.
04:25Now a small thing that you have noticed here. I had inserted it in India and then I had inserted
04:31it in China. But here China came first, then the US came, then India came. So hash maps are unordered
04:38maps within Java. Unordered means that no order follows. Hash map is not necessary that the data you have first
04:46input or the key value pair will get you first out of the way. Hash map in order is unordered.
04:54It doesn't need to be sorted order, ascending or descending, it can be ordered. It means that randomly your data
05:01is stored in half map. So it doesn't need to be inserted in the data. So that's the way you
05:08get inserted. And this is the operation of the insertion. This is also my constant time operation.
05:12Next we are going to getOperation. GetOperation is also my bigof1 operation in constant time. What do I need to
05:21do for get? I write map.get. We use .get method.
05:28and we pass our key. I need value. We will pass that key as a parameter. If I need India's
05:38value, I will store it in the name of the population. What will happen in the population? We will print
05:47India's value. We will print the population. Save and now run.
05:58So for me, my map is also printed. With India's value, the key of corresponding key is equal to 100.
06:06Now, if I pass a key in get, which doesn't exist in my map, in that case, my hash map
06:13will return null value to me. Meaning, if I print hm.get and here, I have stored India, China, US,
06:25but I pass here Indonesia. So, for Indonesia,
06:28If the key exists in this case, I will return null value in this case. So if the get function
06:35is valid, we will return the right value to it. If not, we will return null value to it. The
06:43next operation, which we will talk about, is called contains key.
06:47What does contains key? Contains key is checking if my map exists in my key exists or not. What does
06:56get mean? Get is straight and the value is missing. But contains key is giving you true or false return
07:01depending on whether it exists or not. This is also our constant time operation. How do we call this? I
07:10will print HM.contains key
07:14So, in the contents, C value is small and K value is large. In this case, I will pass a
07:18key in India. In this case, it will give me true return. That means, here it will be true. And
07:26if I have written map.contains key in Indonesia, which is not a valid key in my map. In this
07:34case, I will return false. Let's do this safe and run.
07:42So we have already seen these values. The first return is true. Why? Because India's key exists in the map.
07:49In the map, these three data exist in my map. And then I have print two is false because Indonesia
07:54doesn't exist in my map. So we can check directly on any key basis. What this key's pair exists in
08:01my map or doesn't exist in my map. Next operation which we will talk about is my remove operation.
08:08What does the remove operation do? If there is a valid value, meaning if we pass key to our map
08:14and delete it, then that key and value will delete both pairs. Let's say here I have made my map
08:22structure and I have many nodes inside it. One node is which I stored in data string as India and
08:30its population I stored in 100. The other data is which I stored in China and its population I stored
08:35in 150.
08:36The third data I have taken is US which I have stored in population as 50. The fourth data I
08:42have taken is Nepal which I have stored in population as 50. Now, what do I do? I write to
08:47remove HashMap.Remove. This is a method which I have passed in which I have passed my key. That is
08:55the key which I have removed and this is also my constant time operation. If I have to delete China's
09:00data, then what do I do? HashMap.Remove and I pass my key inside.
09:07I will delete this whole China node and return value. What was the value for China? The value stored was
09:17150. I will return to 150. But if I have a key pass here, I will return null value. What
09:27do I want to do? We will do comment out and remove. I will write value.
09:37HashMap.Remove. In this key, I will pass the value of my key. I will pass the value as China.
09:46And as the data is deleted, I will print the map again. Let's save and run.
09:55In my map, I already have India, China, US, which was printed here. After that, I removed the key of
10:01China, which was 150. I have printed this value here, and then when I printed it again, it is now
10:12only US and India. I also have a constant time to do this operation. Now, if I pass here, I
10:19don't have a country in my map.
10:25So what happened in that case? In that case, there was a null value and my map was the same
10:30because there was no value from it. So the remove function works in my hash maps. Besides, there are also
10:37many functions. There is one function which we print with the help of size. If I don't know the size
10:43of any point of time, I will delete all these parts.
10:51I have added three key value pairs in the map and then I have printed the map. Now I have
10:56to print the size of the map. So I will simply do hash map.size and save and run. So
11:04we have print size which is equal to 3. So it doesn't mean that the key is 1, the value
11:08is 2 and then the key value is 2 add. The key value of the pair, the node is a
11:13single element. That's why I have print size 3. Another function is to check whether my hash map is empty
11:20or not.
11:21So we give the name is empty and these are the size and is empty functions we have already used
11:28in many data structures. In stack also has the same function, in queues also has the same function, in link
11:34list also has the same function, in array list also has the same function. So we have used them many
11:37times. So now we have to intuitively check that usually in Java we have to check in some data structure.
11:43So we have dot size, object data structure.
11:48emptiness check so I call is empty function. So I will print hash map.is empty. Because my map is
11:59empty, so in that case it will print me. False. And there is another function which we call clear function.
12:04What does clear function do? Clear function leaves my entire map. Which I have stored in the map. If I
12:11call hash map.clear.
12:13As I called this clear, my hash map will be empty and print true. Save and run. So what happened?
12:22I had the whole data in the map. Its size also printed 3. But as I called it clear, my
12:29map is empty and it has printed true. So these are many functions that exist with my maps. And these
12:36functions will be handy. So now we need to make their notes. We need to remember correctly.
12:43specially put function, contains key function, remove function and get function.