• 10 hours ago
Transcript
00:00The next set which we will talk about is my linked hash set. Linked hash set is linked hash map.
00:06In fact, linked hash map is used to implement linked hash set. So this is ordered set. It is maintained
00:12in order. If I have set structure in which I have added many elements. First, I have added 1, 2,
00:213 and 4. Now, if I run this linked hash set, I will run the iterator.
00:28So Iterator will return 1, then return 2, then return 3, then return 4. So the order of insertion is
00:37the order of insertion. The order of insertion means that I have inserted elements in my set. It is maintained
00:42in the linked hash set. How it is maintained? Using a doubly linked list. Now I will add a small
00:47point about performance. Usually
00:52The performance of my linked hashmap is slightly lower as compared to hashmap. And the performance of my linked hashmap
01:00is slightly lower as compared to my hashmap.
01:06we perform some extra operations for our order to maintain, which is due to the performance is slightly lower, but
01:13the performance is not too low that the time complexity will change. That's why the time complexity remains the same,
01:19meaning the time complexity remains constant in operations, but in some places, the performance will decrease. So if I have
01:26a choice that I don't need the order of insertion, then prefer that you use the hash map or the
01:31hash set,
01:34to solve this question, I need to maintain the order of our elements. In those cases, we will use LinkedHashMap
01:41and LinkedHashSet. So, we are going to write them code and see how it actually works. I have already created
01:48a cities name in my code, which I have added a lot of cities. Now, I will create LinkedHashSet. LinkedHashSet
01:56of strings, which I will add, which I will give the name, LinkedHashSet is equal to new.
02:04LinkedHashSet. Now I am going to add the same cities. LinkedHashSet and TreeSet operations are the same. The internal working
02:14of internal work becomes a little change. So the operations of the hash set are LinkedHashSet. Both operations are the
02:19same. So I can add the same values. Here I will write Delhi. In fact, I will copy and paste.
02:29Here I will have LHS. This will also come. LHS. This will also come.
02:36LHS. LinkedHashset also store my unique elements. So, the properties in the Hashset, they come. Plus additional order maintain property.
02:45Now, what am I going to do? I will print both of them. Here I will print my cities and
02:51here I will print my LinkedHashset. I will save and run this one.
03:01So in the beginning, my hash set has printed my cities in random order. But the linked hash set has
03:07given the order that was in the insertion. So first, Delhi was printed, then Delhi was printed. Then Mumbai was
03:14printed, then Mumbai was printed. Noida was printed, then Noida was printed. Bangalore was printed, then Bangalore was printed. So
03:21the linked hash set has maintained all the order. Now all the operations that we performed on the hash set,
03:26we can also do linked hash set.
03:32If I have removed the delhi, then I can print the linked hash set. This time, I will remove the
03:41delhi from delhi. If I have to add the iterator or for loop, then it will be the same process,
03:47the same code will be written in the hash set.