• 10 hours ago
Transcript
00:00Next, let's talk about hash sets. So, hash set also has a data structure within Java. Now, in Math, we
00:07will study a set name, which students come from non-math background. I will tell you, what is set? Set
00:14is a collection of elements. Collection of numbers or objects, we can say anything. So, I will say this is
00:21a collection.
00:22of my elements and elements are all unique elements. So, after sets, we did a lot of times in math,
00:30we had a union and intersection concept in math. So, for now it is important that in math, there is
00:38a collection of set in math, where all elements are unique. So, for that, quality and property, if I want
00:45to replicate in Java, then for that, which we use data structure, its name is hash set.
00:52Hashset is a map like a set which has a collection of many elements. And all the elements are unique.
01:01Unique means that there are no duplicates exist. So if I add in Hashset 1, 2, 5
01:10So what will add in the hash set? First one will add, then two will add, then five will add,
01:15then eight will add. All these will be added, but when one and two will be added. So one and
01:21two will already exist. So these will not be added again. So the size of hash set will be equal
01:26to 4. That means it is only unique elements. Hash set is unordered with this. Like hash map was unordered.
01:34You will find it like hash map.
01:38and hash set. There are many parallel places. Like one or two. In the same way, LinkedHashMap and LinkedHashSet are
01:47the same. And TreeMap and TreeSet are the same. Because their properties show a lot of similarities. In fact
01:55This will be very interesting to know that the hash sets are used in the hash map. Internally, there is
02:02a set in Java, there is actually a map. Where my key is my normal element and value is a
02:10random dummy amount. So basically, a hash set is implemented using a hash map in Java. If you ask the
02:16interviewer, you can directly tell that hash set is implemented in the hash map.
02:23The linked hash set is the linked hash map. The tree set is the tree map. So these properties follow
02:30in Java. So this tree set is unordered. It is not necessary that if you have 1, 2 passed before,
02:37then you will have 1, 2 passed before. If we have iteration on this, then 8 passed before, then 2
02:44passed before, then 5 passed before. So this is unordered. And null value is allowed in a hash set.
02:51So if I want, I can add the null value in my hash set. This will be a valid value
02:57which can be added. So hash set's basic work is to store unique values.
03:20Now let's talk about the operations of hash set We have to import import java.util
03:32. . . . . . . . . . .
03:57contains which I can check that in my hash set, no key exists or does not exist in my hash
04:03set. And then I can remove any key from my hash set. Now hash set of implementation, I already know
04:12that it is a hash map of help. So if I have included in my hash set, then I will
04:20visualize them in this way.
04:22But in Java, what will internally become a hash map, which will be 1, a value will be 583, a
04:30dummy value. Then 2, 8, 96, then 3, 4, 86.
04:36So next time, when I try to re-insert 2 again, what will I do in the map? You will
04:43know that 2 already exists in the map. That's why we don't create duplicate keys inside the set. So what
04:49will I do? We are going to define the set of operations for the operations. And we will run these
04:53operations. Now, first of all, I have imported my package into the main function. I am going to define a
05:02hash set.
05:03I will write hash set. I will store some integer value. I will name set is equal to new hash
05:11set. Now I will add some elements inside set.add to set.add to set.add to set.add to
05:23set.add to set.add to set.add to set.add to set.add to set.add to set.add
05:26to set.add to set.add to set.add to set.add to set.add to set.add to set
05:28.add to set.add to set.add to set.add to set.add to set.add to set.add to
05:30set.add to set.add to set.add.
05:33So after adding 5 elements, in my set, there are 3 elements that are unique. 1, 2 and 4. These
05:42which I have added 2 and 1, these are actually added. What can I do? If I want to check
05:49that if my set contains my element 2, then I can check that. In this case, I will print. Set
05:59contains. And if I do not,
06:02take else case, which I will check for, or then I will add another if, if set.contains 3, so
06:14I will print, set contains 3, so if set inside 2 exists, then set contains 2, but for 3, value
06:29exists, so in that case, I will print anything, or set contains 3, now I can remove the elements,
06:36I will remove this line. What will I do with the line? I will remove the line from the set.
06:43I will check if the set contains the two. I will save and run.
06:51This time I printed 1, 2, 4 and as I removed it, this statement will be false. That means, I
06:58added 2 in set 2 times. But if I removed it, then 2 is removed. And this time, set.contains2
07:06will return false value. Now, on the set, there are additional functions that we can perform. If I want to
07:13print the size of my set.
07:16Here I will write set.size, so my set will print my size from my set. I will do it
07:23one time, save and run. My set of elements are 1, 2 and 4, and my set will print my
07:29size from 3. In the same way, if I call set make clear function, then my set will be empty.
07:38Set is empty, and after set is empty, its size is printed as 0.
07:43Besides, I can call another function which we call isEmpty. So if I have called isEmpty, then in that case,
07:50my set will return true if my set is empty and false will return true if my set is empty.
07:55So let's do this save and run. Since my set is clear, the set is empty, it will return true.
08:05So my output is 0 and
08:07So, in this way, I can perform many operations on my set. Internally, we have visualized the set, which is,
08:14that they store in the element node. Internally, hash map is implemented in the form. And the elements always remain
08:21unique. You can also not store duplicates after trying to do it.