• 2 days ago
Transcript
00:00Now there is a little code here, I will explain some parts of the code. I have imported my package
00:08on it. Here I have made a class called HashMapCode. I am going to write my whole HashMapCode. My main
00:18function exists in this class. Now in HashMapCode, I have made a name of HashMapCode.
00:25What does my HashMap class do? My HashMap class, I am going to do my internal implementation of a map.
00:32Now I have written some things here. I have written an angular bracket K and then V.
00:39This is what we call generic in Java. Generics parameterized types, meaning hash map in my hash map can be
00:49string, boolean, integer. For the value store, the type can be boolean, string, integer, float, or float. Now, what type
01:02of hash map and value is fixed for me. So, when the type is fixed,
01:09which is general for everything. It will also work for Boolean, for Integer, Float, for any value, for any type.
01:17So, I'm saying that in HashMap, there will be two parameters.
01:23When the object is created in this class, there will be two parameters. One parameter will show the value of
01:30the key and the value of the key. One parameter will show the value of the key and the value
01:35of the key. In this way, we use the generics in Java. There is not a fixed type of hash
01:40map. You can define any type of key and any type of value. In the hash map, there is already
01:46a node class.
01:48What is this node? Each bucket of this node, which are linked list nodes, is stored here. So I have
01:56written a private class node. This is a private class, so no need to use it outside. There are two
02:01values in the node. One is a key and one is a value. This means this is my pair type
02:05class. What type of key is here? It is K. In the hash map, whatever key type has passed in
02:11the constructor, in the constructor time, that is my key type and value type is V.
02:16Besides, I have made a constructor for the node, which will be the key value and the value value. As
02:23the key value pair comes, I will initialize it. So, this is my private class node. With this, I have
02:30made a property by the name of size. What is it? It is called small n. The size is my
02:35total number of nodes which exist in my hash map. Besides, I have made a bucket. What is it for
02:41me? It is an array. If I make a normal array, I usually write.
02:45private integer ARR. So I used the same syntax for a link list array. What is the name of the
02:53array? I have given the name of the array. Here I have put square brackets here. I know that my
02:59array is the array of buckets. What is the array type? In the integer array, we write int. Here the
03:05array type is linked list. What is the array stored in this array? It is linked list. So I have
03:11written the array type here. Link list of nodes.
03:14So here I have created two values. One is my parameter size and one is my parameter buckets. These buckets
03:20are actually my hash map. What is the hash map? My array of link list. So the length of this
03:29size will be equal to n which will be equal to buckets.length.
03:35Now here I have a small sign here. What are the different versions of Java? There are different editions, different
03:40versions. So in some versions, you don't need to write this line in your code. In some versions, you have
03:47to write it in your code. Why do we write it in some versions? At the rate, suppress, warnings. I
03:53will discuss about this one or two functions. After that, I will tell you why it will need to write
03:57it in your code. If you don't have code, well and good, you can write it in your code.
04:04After the hash map, the public hash map has been created by the name constructor. What has done in the
04:08hash map? The bucket has been initialized with the bucket and the size.
04:14I made a bucket of size 4. In the beginning, my bucket size is equal to 4. And in that,
04:22I have typed a link list. And then, I created a link list on one index. Until I don't create
04:28this link list, I will have only a empty array. And as I created a link list, I will have
04:35a empty link list on each index. I will explain a little difference between these two things.
04:43If I have created my bucket, I mean is equal to new linked list of 4 if I wrote there,
04:49that means that I have a bucket created within 4 index. But if I go from one place, I will
04:57initialize from the left link list, it will also be a linked list.
05:00There will be a link list here, there will be a link list here, there will be a link list
05:04here. Yes, this link list will be empty in itself. But I have to make a empty link list everywhere
05:10so that I can add directly elements. Otherwise, I have to check in the put function that I have to
05:14have a link list on the index or not. In this way, I have initialized from the empty link list.
05:20This part of my code is already given. What do I have to do? I have to give a put
05:24function, which is empty function. I have to give a contains, which is empty function. I have to implement the
05:28remove function.
05:29get implement, keyset implement, all my functions are 4-5 functions. They perform in constant time, this is also constant
05:37time, this is also constant time. All these functions I have to write actually their implementation. So how many parts
05:43are given? I have given my HashMap class, node class, constructor, size, bucket. What did I do in the HashMap?
05:51What did I do in the constructor? I have initialized the size from zero. I have initialized one node and
05:55buckets as a linked list of size 4.
05:59I have initialized a link list in every index Now, why is this suppressed warning? In some versions, you can't
06:07write a link list here You have to show a type of link list If you want to create an
06:12area of link list, then what type of link list is in that area? That will show us So, what
06:18will Java do? It will not run your code If you simply write this line in the area, then your
06:24code will not run in some versions There will be errors
06:28It is not an error, it is actually a warning, but the code will not run in VS Code. What
06:34did I say? Suppress warnings. Suppress or not, it will not come to the warnings.
06:43This error is the unchecked type. You have not checked in the link list, which type of area is going
06:49to form my link list. We will suppress the warning that we will not get attention to it. So, when
06:55we will write the link list in our code, we will simply add the rate, suppress warnings, unchecked. That's why
07:02it was the logic. Now, let's implement our put function. To implement our put function, the first thing we need
07:09to find out is our bucket index.
07:15I will assume that I have a hash function that exists in the hash function. If I pass my key
07:23in the hash function, what will I do? I will return the hash function to the hash function. I will
07:29return the hash function to the hash function. It will give me a hash function. I will name the hash
07:39function.
07:42Now we don't implement it, we will implement it after we have key passed in it. Every time the key
07:48and value type will remain K and V because we don't know if it is integer type, string type, boolean
07:53type. Once the put function has a bucket index. What is the other job? The other job is to search
07:58on the link list. What do we need to search on the link list? To return the index of data.
08:04That means if I have my link list
08:08I have two bucket index. What do I have to do? I have to search here. I will go to
08:14this index, then I will go to this index, then I will go to this index, then I will go
08:16to this index. If I had to search for Canada, then I got my data index. Where I actually have
08:22to store my data. So I have to take this index. Which node we are talking about? I have to
08:30take it. So I will create another function that will give me data index. I will give it to search
08:35in link list.
08:38I will pass my key and wherever the key will be,
08:42the index will return me. There are two cases. There is one case that I will get a valid index
08:48in the search link list. I had to put Canada and Canada already exists. So I will get a valid
08:53index. The second case is that Canada will now be put and Canada is not in the map. In that
08:59case, I will get a minus one return. So I will get to know that now I have to update
09:05the value to my data index or create a new pair or new node.
09:11So first, let's complete this hash function and this search and link list. What did we talk about hash function?
09:17We are going to use hash code name function. I mean, I have some value in hash code. How will
09:24it come? I will call hash code for which key.
09:29So, I will write key.hashcode. So, what will happen? This hashcode function is called hashcode. For any object, for
09:36any value, it will return hashcode to any object. It means it hashing and it will change the form and
09:42I will get a value. Which type of value will get? I will get an integer value. Because the return
09:48type of this function is an integer. You can read the description here that it returns a hashcode value for
09:54the object. So, you can take any object and return hashcode value to any object.
10:00The data structures are used in Java and we are also implementing HashMap. It is used in HashTables. There are
10:09more details about this. So if you are coding in VS Code, you can learn many things. If not, we
10:15can go online and go to Google. Basically, how do we do this HashCode work? We don't have any interest
10:19in it. It works and gives us some code to return it. Now it can be that it is code
10:25123456780.
10:27It may be that code is minus 1, 2, 3, 4, 5, 6, 7, 8, but what I need? I
10:32need index. I need bucket index. So what do I need to do for bucket index? First, whatever code comes,
10:38I will remove absolute value. If it is negative, then convert it in positive value. First, I will write math
10:45.abs. I will pass into my hash code.
10:50What happened to ABS? I have absolute value of my hash code. If it is positive, then it will be
10:54positive. If it is positive, then it will be converted. Now, the absolute value came, I need an index. And
11:01the index value of the value of the value of the value of the value of the value of the
11:04value of the value of the value of the value of the value of the value of the value of
11:06the value of the value of the value of the value of the value of the value of the value
11:10of the value of the value of the value of the value of the value of the value of the
11:13value of the value of the value.
11:24If I have taken a module with 4, if I have taken a module with 4, then the value is
11:31always from 0 to 3. This is a very basic math thing I have done and I have already done
11:37it. What will I do? I will remove absolute value and then I will take the remainder with size and
11:43I will return this value. This value will be guaranteed from 0 to 3.
11:49because I have done with my size. So this way my hash function will complete. Now let's talk about search
11:55in link list function. Now let's change here another little bit of change. I had a public hash map in
11:59the constructor. I had initialized size with 4. Because my link list was 4 sizes. So this is my hash
12:07function. Now I have another function called search in link list. Private. This will also return index. So int is
12:14return type. Search in link list. This also comes in my key.
12:19Now I am going to run a loop on the link list. With this I will pass my bucket index.
12:24I will get to know which index link list I have to approach. So I am going to store it.
12:30I am going to say link list of node. I will say link list is equal to. I am going
12:40to store the link list here. Now I am going to run a loop on the link list.
12:48So I can use the loop for i is equal to 0, ll dot size, so we can use for
12:53each. If I use i is equal to 0, i is equal to 0, i less than ll dot size,
12:59that will show something like that. I++, I will check every time, in the node, which will also be my
13:07node, like linked list dot, get i, I will check what my node's key is, that is equal to my
13:15key. If I get the key here, then another
13:19I take a variable as data index which I will initialize with this 0. So I will return my data
13:25index from here. If my data index will be plus plus and if I didn't get my last key until
13:30last then I will return to minus 1. So simply I have done what to search in the link list.
13:36I have also left the link list in the notes.
13:39Go to buckets of BI index. I have initialized the data index with 0. I have looped on the link
13:45list. I am going to check in one node. First, I have removed the node. Then, I have compared the
13:51node with my key. Then, if I match the value there, I have returned the data index. No, I have
13:56done it with DI. If I don't get my node, then I will return it with minus 1. So, I
14:03have completed these two functions. I have implemented our put function.
14:06Here I have my values. Now there are two cases in search index. Either the data index will be a
14:12valid value or not. Let's take a valid value. That means the data index is not equal to minus 1.
14:18That means the key already exists in my linked list. What do I have to do in that case? I
14:23have to update the value. So first I have to remove the node. Node is equal to my bucket. By
14:30the index of my linked list.
14:34I will get my data index node in the bucket and in the index index. Now, in the link list,
14:43which index? DI index. I have removed the node. I want to update this node. So, node.value will be
14:51equal to my new value. I will update this here. But if that doesn't happen, the value of DI is
14:59minus 1, then I have to create a new node. So, in that case, what do I do?
15:04I give my bucket of bi.add I pass a new node in which I pass my key and value
15:16and size I give my plus plus so buckets of bi means I go to index and in the linked
15:22list
15:23add a new node in which I pass with key and value and add a new node here because there
15:30is a new node added here so it should be plus plus so my put function is complete now add
15:35a small thing in this way, let's see if the time of rehashing is coming or not, I want to
15:41check that thing so I have the value of lambda here every time the size changes, so lambda changes too
15:48In fact, this is my size, this value is different. What do I take? I take it as small n.
15:54Here I have small n. Here I have small n. Here I have small n. Here I have small n.
16:00Here I have small n. Here I have small n. Here I have small n. Here I have small n.
16:05Here I have small n. Here I have small n. Here I have small n. Here I have small n.
16:11This will be equal to buckets.length.
16:16This size will be different, this is my size capital N. In fact, I can create a variable here, private
16:24int capital N and here I will initialize this dot n to this small n, here my capital N initialize
16:35will be 4
16:37So I changed two different variables for simplicity. One is small n and one is capital N. As my new
16:44array will become the size of N, which is my bucket size. Completely correct. Now when I have to divide
16:51with hash function, I will divide with capital N. Why? Because the bucket index should come from 0 to size
16:57minus 1. So we will take N from N.
17:00So here when I have to remove lambda, I will add small n here, like the new node has added.
17:06I will divide with small n to capital N. And the lambda will not be integer type, it will actually
17:13double type. Double Q, because there is a decimal value in it, so I have to double type it too.
17:21So if I have 2.25, it will not be 2, it will show greater value from 2. So I
17:27will compare what my
17:30Lambda value greater than my threshold value. For example, let's take the threshold value for example 2.0. If this
17:37is greater than the threshold value, then what should I do in that case? In that case, I have to
17:40do rehash. Let's write a function to rehash. Let's write a private function. Private, void, rehash.
17:50What is the work of rehash function? The work of rehash function is to make the whole data structure in
17:56hash map more optimized. What will we do now? The current data is stored already somewhere. I mean I make
18:04an array. Link list of node type. I made an array called old bucket. I store my buckets in the
18:14old bucket. Let's put square brackets here.
18:17Now what I will do? I will completely empty my buckets. So I will do new linked list of size
18:24N multiplied by 2. So the first bucket size I will multiply by 2. So my bucket size will double.
18:34And here I will do my N value.
18:37increase and double, so my size has doubled here, so I have created a new bucket and what is empty,
18:43now what do I have to do? I have to remove the elements from the old bucket and put it
18:46in the new bucket. First, the empty bucket is made, let's initialize with the empty linked list, for int i
18:55equals 0, i less than buckets dot length i++. What do I do every time? I do every time, buckets
19:04of i is equal to new
19:07Link list. I have initialized it from a blank list. This is all my initialization process. In fact, I can
19:14create a different function for this function. We can do it in this function. Now, what do I have to
19:19do? I have to add all my nodes. And I have to add nodes in my bucket. Or in a
19:26new bucket. So, I will run a loop. for int i equals 0, i less than my old bucket's length,
19:34i++. And now,
19:38I am going to loop on the index of the bucket. I am going to loop on the index. I
19:43mean, I will have a link list first. Let's take a link list of node. Link list is equal to
19:50my old bucket of i for int j equal 0 j less than link list dot size j plus plus.
20:03Every time, I will remove my node from your link list, or link list dot get. In fact, I can
20:08remove it from here directly.
20:10I will remove all nodes and then what will I do? This node which I have removed from the previous
20:18bucket I will put it in a new bucket. So simply I will pass the put function into node's key
20:24and node's value. What happened in rehashing?
20:29Link list, I have stored my hash map in the old bucket. I have initialized my current bucket in a
20:36new series with double size. Now, I have initialized all buckets and indexes in a empty link list. I have
20:46initialized all nodes from the old bucket and put it in a new bucket. This is the process of rehashing.
20:56I will give a brief again. This is my put function. First of all, I have removed the bucket index
21:01and then the data index. How did it get out of the hash function? The hash function has given directly
21:06the bucket index. Then I searched the link list and then the data index. After that, I have seen that
21:10I have to update the bucket or update the node in the bucket. I have added the value. Then I
21:16have compared to the lambda function. If the lambda is bigger than the threshold,
21:20HashMap can you rehash? No, there is no need. So, in this way, we write put function in our HashMap.
21:27So, what am I going to do? I can define HashMap in my main function. HashMap of, I will pass
21:33first here. Key will be string, value will be integer equal. I will write HashMap is equal to new HashMap.
21:40So, in this way, my new HashMap has defined.
21:43and here I have not implemented a hash map. I mean, I don't use inbuilt. I use my hash map
21:49class which I have built in here. I can put elements in this element. What I can put in the
21:56map, India for 100. We can put in hash map in China for 150. We can put in hash map
22:07in US for 50 and
22:14Let's put another value, Nepal for 5. So in this way, my data will also be put in my hash
22:21map. Here are my key values, here are my values. Now let's talk about our other functions. Next, which function
22:28we will implement, let's name contains key. That means this function will find out if any key is in my
22:33map or not. So in that case, what do I have to do? Let's discuss a little bit the process.
22:39If I already have my map exists,
22:44Let's assume that this is my hash map where many keys already exist. And if I want to give a
22:49key if I want to search for Nepal, that the key of my hash map exists in my buckets, linked
22:56list notes or not?
22:59So, first of all, what will happen? First of all, let me take a look at where Nepal should be.
23:04Who will tell me which index should be, which bucket should be? That will tell me my hash function. So,
23:09who will do my hash set? That is why this chapter name is hashing. Because here hashing is used everywhere.
23:19HashMap has implemented in the hash function. Hash function will tell its BI for Nepal. It will tell its bucket
23:26index.
23:27So here, if I have passed the hash function in the hash function output, I will return 0 to the
23:34hash function output. If the hash function has given 0 to the hash function, what do I have to do?
23:37I have to search zero in the bucket. I will go here and search on this node. Then I will
23:42go here and search on this node. Here I will get the Nepal key. So what will I do? In
23:46this case, I will return true. But let's take another example, key is equal to
23:52If I use italy example, do italy exist? So, let's assume italy has passed my hash function and italy has
24:00returned me a parameter to italy which is key, its bucket index should be 2
24:07So what will I do? I will go to this bucket index 2. I will see what exists in this
24:11link list node. It will not exist in Italy. Then I will go to next. What exists here? It will
24:17not exist here. Then I will go to next. What exists here? It will not exist here. So if it
24:22does not exist in this bucket index. That means my key is not in the whole hash map. So what
24:29is the process? The process is the same. What will we do first? We will try to remove bucket index.
24:34Then we will try to remove data index.
24:37And if you don't get it, if you don't get it, then there will be a minus 1 index return.
24:42If you get it, then there will be a valid value return. So in this case, I have to return
24:48false. If I return false, then I return false. So I will return true. Let's write code for this. My
24:55contains key function is what I will do. I will remove this return statement. And I simply put the code.
25:03I will copy and paste it. The same thing I have written. First, I will remove bucket index using this
25:08hash function. After that, I will search in my linked list. So, I will pass the key and the bucket
25:14index. I will have my data index. If the value of the data index is not equal to the value
25:18of minus 1, it means that a valid value is returned. So, what do I have to do in that
25:24case? I have to return true. That means that my key exists. If there is minus 1, then I have
25:31to return.
25:32False. So this simple code will be implemented in contains key. Now what can I do in this way? I
25:39can implement this get function. What can I do in get function? It will return value. Where the key exists,
25:45the value of the key will return. So simply, I will copy the put code and paste in get function.
25:55First, we will search the data index. If the data index is minus 1, it means that my key already
26:02exists. If the key exists, then the node is removed.
26:07As we have put me, I have simply removed the node. Now I have to return the node's value. I
26:13don't need to change the get function. What does the get function do? The get function gives me the key
26:17to the value. What do I do? I will remove the node where the key exists and return the node
26:22.value. And if there is minus 1 and the value exists, I will return the value in that case.
26:29So I have the get function complete and you can see that the same logic follows all of them. First
26:35we have to remove bucket index and then we have to remove data index. First we have to remove bucket
26:38index and then we have to remove data index. The same work we are doing but we have to tweak
26:42the logic here. So it is very simple to implement hash map. We have to implement this get function. Now
26:48let's talk about remove function. How do we remove our key corresponding node from the link list?
26:58to find the key value pair. So I will write the same code first. I will remove bucket index and
27:03then I will remove data index. If it gets caught, then remove the node. How do I do this node?
27:08I will remove the node here. I mean, where to get it, I will remove the node. And then what
27:16to do directly? I will return the node value here. I mean, which key we are going to remove, we
27:23will return the value.
27:25It is a very simple logic. First, I have to find the node which I have to delete. Then, in
27:29buckets of bucket index, I removed the link list directly from the di index node. And then, as this remove
27:37function has returned the link list's remove function to the node. So, it has also returned the node here. It
27:42will be stored here. And I have returned the value here. And if the node exists, I have to return
27:46the value. And if the node exists, I have to return the value. So, it will complete my remove function.
27:52How many functions I have completed?
27:54A function which I have completed, that is my put function. What is the time complexity? Its time complexity is
28:00big of lambda to equal. But because lambda is always small with constant value, this will be constant time. If
28:08we talk about contains key, how much time it takes to contains key? I also have constant time. Or we
28:15can say lambda to equal it. Remove function also takes time for big of lambda or big of 1 which
28:21is constant time. Get also takes time for get which is my
28:24constant time. Now let's talk about key set, where I have to return my array list. Here I will create
28:32an array list, array list of. I will call strings of this, because keys type is not string, I will
28:37call k. Because my keys have no fixed type, k type. I will create my keys set is equal to
28:47new array list. Now I will do all the
28:51to go to the keys and go to the keys and store them in the array list. So, I will
28:56run a loop for int i equals zero i less than my buckets dot length i plus plus. Okay, before
29:06completing the keyset, one small thing we have written remove.
29:10Before we return, what do we do? We are going to do our size, i.e. small n. We are
29:14going to do our size, i.e. small n. We are going to do our size, i.e. small n.
29:15We are going to do our size, i.e. small n. We are going to do our size, i.e.
29:15small n. We are going to do our size, i.e. small n. We are going to do our size,
29:15i.e. small n. We are going to do our size, i.e. small n. We are going to do
29:18our size, i.e. small n. We are going to do our size, i.e. small n. We are going
29:24to do our size, i.e. small n.
29:40Now I can add for each loop and for loop. I have run a loop on my link list. I
29:47have left each node and now I have to add node key to the keys. What will I do in
29:56last? I will return my keys. What will I do in the key set function? The key set function will
30:02store all keys in a carry list and return them. Another function is empty.
30:10We will know what is empty is empty boolean type and it will work to tell if my hash map
30:17is empty or not. This is the most easy way to implement. If my total nodes number is zero then
30:23my hash map is empty and if it is greater than one then my hash map is empty. So I
30:28will check return n equal equal zero.
30:32If I don't have one node, it means that my hash map is empty, so return to true is empty.
30:38No, return to false. Let's do this whole code and save. We have implemented all the functions. Now, in the
30:45main function, I had put the elements put. Now, what am I going to do? In an array list, I
30:52am going to store my array list of string. So my keys will be equal to, I will call hash
30:58map.
31:02key set, so all my keys will return all my keys to this loop, so all my keys will return
31:08all my keys to my loop, so all my keys will return all my keys to my loop, so all
31:16my keys will return all my keys, so all my keys will return all my keys, so all my keys
31:19will return all my keys to this code, let's do this code, save and run, so I have some warnings,
31:25why do I have this warning, so when I called my rehash function, then I have linked list of
31:30N into 2. So for the linked list of N into 2, if I want to hide these warnings, then
31:36I will do Suppress Warnings for Unchecked. If I just suppress warnings, if I don't see warnings, then I will
31:45simply remove them. So what happened? My hash map has created here, in the form of lines.
31:54In the hash map, then I have called the keyset. In the keyset, first my Nepal was printed, US was
32:00printed, India was printed and China was printed. Now, if I want to print a key for a value, like
32:06I want to print, hash map.get India. So, what should I print for India? 100 was printed. So, the
32:14same value I have printed, which is 100. Now, I will delete India. So, I have deleted hash map.remove
32:22For India, now I am trying to do what I am trying to do again. I am trying to call
32:27to get India.
32:32So first, my hash map is printed. After that, I have India's value printed. Then, when I tried to remove
32:38India, the value has returned and it has been removed. After that, what happened last? It has returned null. Because
32:45now, India has removed the hash map. So, if we put a call for India, then I have a print
32:50null value. So, in this way, the rest of the functions, the rest of the methods, we can also use
32:55call and test how it is going, how it is going. So, we implement a hash map.
33:01We have seen bare implementation, we have understood why constant time comes, how many functions are implemented and we have
33:09seen it myself. There is a similar structure that Java has implemented. In fact, we have implemented it like that.
33:18We don't have to do that here. When we have questions, coding tests, we will go to the platform and
33:22solve it. In fact, in the interview, when we are solving it, we have to use existing implementation already. We
33:29don't need to make it from ourselves. But yes, it may be possible that you have an interview and you
33:33can say that you implement it with HashMap. So the question of the implementation of HashMap, in fact, recently when
33:40I am recording,
33:41in this lecture. So in this recent month, Uber is going on and a few students told me that there
33:47was a question of the implementation of Hashmap there. So it's not that this implementation is not important. It's not
33:53just understanding. It's also important from the interview perspective and we have to do it properly. How does each function
33:58actually implement?