• 10 hours ago
Transcript
00:00Next time we are talking about Linked Hash Map. In the beginning, we talked about our maps are three types.
00:06One is my Normal Hash Map which we have talked about in a lot of detail, implementation and functions. Another
00:14is my Linked Hash Map. The third is a Tree Map.
00:19Now we are going to talk about linked hash map. Linked hash map is like hash map, but there is
00:25a special property. Linked hash map, the order of the keys, is according to insertion. What order you have put
00:35in the keys, that order you will get the keys. Linked hash map, we will write something like linked hash
00:41map. We also have to import import java.util
00:50or import linked hashmap or import all the classes and interfaces. I have written linked hashmap. It will also pair
01:00its key and value. So here we will define key and value type. Is equal to new linked hashmap. And
01:07also there are same operations. Put operation will be the same. Get operation will be the same. Remove operation will
01:14be the same. Size operation will be the same. Is empty. It will be the same.
01:18We will also start with the key set as before. All operations will remain the same with the same time
01:25complexity. What does the link hash do now? It performs in constant time. If you have to maintain the insertion
01:33order, you can maintain the insertion order.
01:37which has been added to you, which has been added to you, which has been added to you later. Let's
01:43write code for this. I have imported the Java Utility Package. In the main function, I am going to create
01:48a linked-hash-map. So I will write linked-hash-map. I will make it a string, integer type. LHM
01:58is equal to new linked-hash-map. Now I will add some key and value pairs.
02:08First of all, I will put my key in the linked-hash map. I have put my key in India.
02:13After India, I have put population store 100. In the linked-hash map, I have put in China. I have
02:20store 150. In the linked-hash map, I have put in US. I have store 50. You will find that
02:28every time we are working on population data, when will we see the real problems?
02:32questions are going to solve their own problems, so you will get a little change from population graph. I am
02:39going to print a linked hashmap. Let's save it and run. Java. So what happened? First I passed India, so
02:49India was printed. Then China passed, so China was printed. And then US passed, so US was printed. You will
02:55say, Didi, this is a coincidence. So let's compare it with the hashmap. I am going to write in the
02:59same code, which is for the hashmap.
03:03So when we compare it with HashMap, it will be clear that we are talking about what we are talking
03:07about. I will give it to HashMap. Now I will print it here, my HashMap. One time I have printed
03:15a HashMap and one time I have printed a Linked HashMap. When we compare both, we will know that this
03:22is my HashMap, which was printed first, which is HashMap. There is no order here. First India passed, but here
03:28is China.
03:30Then China passed here, US is shown before. So there is no order here in the Hash Map. But yes,
03:34in the Linked Hash Map, what was passed in the order? For example, India passed, India passed, China passed, China
03:41passed, US passed, US passed, US passed. So in the cases, in the order of my key and value pairs,
03:47nodes are inserted in the order. You want to maintain the order in that order. In those cases, we will
03:53use the Linked Hash Map. Same time complexity, same functions.
03:59Everything is the same, just the internal implementation changes. How does the implementation change happen? First, when we implemented in
04:07Hashmap, how did we do? I have my buckets, I have a link list, I have a link list, I
04:16have a link list, I have a link list, I have a link list, I have a link list, I
04:17have a link list, I have a link list, I have a link list, I have a link list, I
04:18have a link list, I have a link list, I have a link list, I have a link list, I
04:20have a link list, I have a link list, I have a link list, I have a link list, I
04:22have a link list, I have a link list,
04:26There will not be a double link list. In the double link list, we can travel forwards and backwards in
04:31both directions. In the double link list,
04:36forward and backward in both directions we can travel on the notes and with this double link list we can
04:42maintain our order which order has come in the order we can store it if someone has come in the
04:49order then it will be stored in the order if someone has come in the order then it will be
04:51stored in the order then it will be stored in the order so this is an important property for my
04:55link dash map the other things are the same time complexity is the same what I have to do is
05:00use the double link list internally
05:04If you ask in interviews, you don't ask the implementation of the linked hash map, but the hash map is
05:09important. You don't ask the tree map too, but you can ask which data structure is used in the linked
05:16hash map. Then you can tell that it is used double linkless inside and error itself. Or if you ask
05:21about the tree, you can also tell that.