Transcript for:
Understanding HashSet Collection in Java

Hello everyone, welcome you all. In the today's video we are going to see has set collection in Java. So from the previous videos we have understood collection is a root interface and the collection interface is extended into multiple child interfaces. So list is one child interface and set is another child interface and queue is another child interface. So these are three main interfaces which have derived from the collection root interface. Now list interface again implemented by using ArrayList which we have seen in earlier and also LinkedList. So these are the two classes which have implemented list interface. So today we are going to see about set interface. So what is set interface, when we need to choose set interface and what are the different classes are available which have implemented this set interface. So basically set is a child interface of collection and set interface is again implemented by using two main classes. one is hash set the other one is linked hash set linked hash set so these are the two main classes which have implemented set interface so set interface is a shell interface of the collection so now we will discuss more about set interface when we have to choose set interface and has it has set class this is the main class we are going to discuss today and this is the chain class which is derived from the set interface so all whatever methods we have in the set interface which is implemented by the has set class so now let us main focus on has set class so what is basically has set means so when we have to choose has set normally if you want to represent like group of elements or group of object as a single entity where the duplicates not allowed okay duplicate should not allow and insertion order not preserved insertion order not preserved so in these two cases we go for a has set concept so in the has set concept in the has set class here we cannot store the elements like duplicate elements we cannot store and insertion order is also not preserved so what does that mean is in whichever order we have inserted objects or elements into the has set it will not be there in the same order so it will be arrange all the elements or objects in the random order So in the has set duplicates elements are not allowed and insertion order is also not preserved. So these are the main two characteristics of the has set. Now we will discuss a few important points about has set. The first point is as we discussed duplicates not allowed duplicates not allowed and the second point is insertion order insertion order not preserved. and this particular hashSet will insert the elements by using a concept called hashCode. So hashCode is a concept by which the elements will be inserted in the hashSet. So that's the reason searching of elements will be very faster in hashSet. Suppose we have a more number of elements if you want to search a particular element it will be very faster because internally it uses hashCode concept while inserting the elements. So since it is not maintaining any order so there is no index concept in the hash set. So basically it is an unordered list that means all the elements will be arranged in the random order. So there will not be the sequential order. So there is no index concept here. So hash code concept will be internally used while inserting the elements in the hash set. Now heterogeneous data will be supported heterogeneous data will be supported so as we know all the collections will support heterogeneous data that means we can store different type of objects different type of data elements in the hash set and along with this heterogeneous data nulls also support we can also add nulls in the hash set okay so these are the main important points which we have to remember while discussing about hash set so duplicates not allowed insertion order is not preserved and hash code concept will be used at the time of inserting the elements and allow the heterogeneous data and nulls also allowed. Now so when we need to go for hash set if you have search operations like in your operations the more number of operations are searching then go for hash set because searching will be very faster here. Now so this is a child class of set interface. Now let us see how we can create has set objects how many ways we can create a has set object how it is going to store or locate elements so has set object can be created in many ways so the first way i'll show you now something called has set so this is a way hash set hash set and create some object reference variable hs equal to new hash set Okay, so this is a default way we can create a has set object So when I create a has set object like this, so we are calling default constructor here empty constructor. So by default it will allocate 16 locations so the number of locations though these are default Default size or initial size we can say initial size instead of saying default size we can say initial size So initial size means it will by default allow us to store 16 objects or 16 elements. So this is a by default or initial size of asset. But how we can store how many elements we can store is along with this location this is the initial value like 16 locations will be there and 16 elements we can store. And apart from this size or location there will be one more thing called load factor in asset. Load factor. Or we can also call it as a fill ratio. So this is one more important thing in the has set. So we need to understand two things here while creating the has set object. One is default by default it is allocated 16 that is the initial size of has set and we can increase the size also later. And one more thing is load factor and fill ratio. So what is the load factor of fill ratio of has set is 0.75. So 0.75 so this is representing one percentage. This is called as a load factor or fill ratio. Now let us understand what is this load factor and fill ratio. This is most important in HACET. So before understanding what is this load factor and fill ratio let me just give one example from the ArrayList and then we will come back and understand what is load factor. In case of ArrayList normally what happens is when I create ArrayList observe this array list ar equal to right new I'll say array list so I created one array list object like this so when I create an array list object like this by default how many locations will be created 10 locations will be created so here default size is 10 so 10 locations will be created so So if I add more number of elements like say 10 elements I have added or 10 objects I have added in the array list, 10 locations are filled. Then I have added one more element called 11th object or 11th element in the array list. Then what happens is normally it will not directly add a value at the end of the list like this. So internally what happens is it will create another bigger array and default sizes 10 locations will be created. and all the elements in the previous array will be copied into the new object and the 10th element will be added here and this variable now reference to the new object and the old object will be eligible for garbage collection. So every time when you're inserting or when you're removing the element from the array list this object will be created new object will be created. and most of the times whenever these locations are completed or filled with all the elements it is reached maximum size then automatically it will create a new object and the new elements will be added into the new object so this is the exact thing will happen in case of ArrayList. So when this new object will be created as soon as all elements are completed here the locations are filled then the new object will be created. So this will happen whenever you do additions and removing the elements in case of ArrayList. the same thing is happening for has set also. So in case of has set for example let me just take has set concept in the has set what happens is when I create a has set object by default 16 locations will be allocated so let's I can store 16 elements so I already added more number of 16 object like this so now the default factor load factor is 0.75 so this is representing the percentage Now what is the load factor means suppose 16 is a initial size of the has set. So I added all the 16 elements now I want to add another element 17th element. In case of ArrayList once it is reached 16 then it is created a new object but in case of has set as soon as these locations have reached 75 immediately it will go and create another object with the new size. It will create 16 locations plus with the new object location. and then this object is referring to the new object and the old object is eligible for garbage collection. So in case of hash set asunat is reached 0.75 means 75 percent of the locations immediately it will go and create a new object. So this is called as a load factor. So this is most important topic load factor or we can say fill ratio. So the initial size of asset is 16. and load factor is 0.75. So when I create a hash map, empty hash map, by default 16 is the location, load factor is a 0.75. As soon as 75% of locations are filled, then the new object will be created. All the elements will move to or copy into the new object and the new element also will be added. So this will happen whenever you add a new object into the asset. So this is concept of load factor. Now, so can we control this load factor? So, can you define my initial value? Suppose I want to store 100 elements or 100 objects, can we define this or can we control on this or can we configure these two values? So, to configure these two values, we can do that. So, how we can do the configuration of initial value and a load factor of has set? And to do this, we have to use another way of object creation. So, for example, let us say here, I will say have set HS equal to HS equal to here new hash set and here we have to specify so default size is 16 now I want to store 100 elements so I can say 100 so here 100 is a initial size and what is the load factor load factor are not mentioned here so load factor still 0.75 so this is a load factor but still if I want to specify the load factor also then I can say hash set H is equal to I'll say new hash set of locations comma load factor. So load factor is always in the decimal format. Okay, so we can specify initial size and load factor then automatically. So 75 is the default load factor. I can specify 90 or 95 something like this. So now as soon as memory as soon as the locations have reached 95% immediately it will go and create another. locations and all the elements will be moved to the next object. So this is a main concept of load factor and initial location in the has set. So we already seen like has set will accept heterogeneous data. Suppose if you want to restrict that to specific type of data or specific type of object we can still restrict. So for that I can say has set and here in the angular brackets we have to specify what type of data I want to store. Suppose I want to store only numbers. I'll say integer class HS equal to here I'll say new and again hash map sorry has set and in the angular bracket we can specify the type of the class and close the bracket. So here and here we have to specify the type of object what kind of objects we are going to store in the has set. Okay so these are the different ways we can create an object and where we can configure the initial value also we can configure the. Load factor so load factor is very important in the hash set concept. Okay, so now we have understood what is hash set and when you have to go for hash set how the elements are going to store. So where the duplicate elements are not allowed insertion not resolved so not preserved. So there is no index concept here. So now just like other collections here also we can do certain operations on hash set by using different methods. Now let us see what are all methods are available inside the hashSet and different methods are available. And one more thing here we need to understand like hashSet is a class which is implemented a set interface. Set is an interface and hashSet is a class which is implemented the set interface. So hashSet is not having their own methods. There is no special methods in hashSet itself. But whatever methods are there in the set interface the same methods are implemented in the hashSet. other than those methods hasSet doesn't have any specific methods to this which belongs to the hasSet. So whatever methods are available in the Set interface, the same methods are implemented in the hasSet class. Now we'll see what are those methods available and how we can use those methods to perform different operations on the hasSet. So just like other collections and here also we have certain methods. Suppose I want to add some group of elements into hasSet, then we have a method called add. So here directly we have to specify the value. So here we don't have an index concept. So directly specify the value or object that will be added to the hasSet. Add method is available. And also we have addAll method. And here we have to specify some collection of elements and group of elements will be added to the has set add all is there and we also have a remove method which will and in case of remove method here we cannot use index because here we don't have index concept so directly we have to specify the object name or value which have the which is there inside the has set collection and accordingly that value will be removed and similarly we can also remove multiple objects by using remove all so here we can specify the collection and the group of objects will be removed from the hasSet so add, add all, remove and remove all. Okay so these are the common methods which have apart from this and whenever use add method we can just pass only value so here we don't have any index concept because in hasSet elements are arranged in the random order so there is no sequential manner it will not store so all elements will be added or stored in the form of random so randomly arranged so here We cannot do any insertion and updation of the elements or objects. But in case of ArrayList, we can do that. But here we cannot do that because we don't refer any elements by using index. So these are the common methods we have. So apart from this, we also have a contains method. So here when I specify any object or element which will check the element is present inside the collection or not. If the element is present it will return true, if it is not present it will return false. And similarly we also have containsAll method and here if you specify group of elements it will check all the group of elements are present inside the has set or not will be written. And apart from this if you want to check particular collection or has set is empty or not we can have isEmpty method which is available. So this butler method will check if it is empty, it will return true. If it is not empty, it will return false. IsEmpty method is also available. And along with this, we can also read all elements by using for loop for each loop is also there. And by using iterator method also, we can read all the objects, all the elements from the has set collection. Okay, so these are all different things we have in the has set. And one more important thing is in case of list or array list, we have done searching, sorting and shuffling and all. Here, By using contains and contains all method we can do searching operations but sorting is not directly possible here. Sorting and shuffling is not directly possible. If all elements are arranged in the sequential order then sorting and shuffling is possible but here we don't have an index concept the elements are arranged in the random order so we cannot directly sort it. If you still want to sort it we have to convert this entire asset into array list and then we can do sort method by in the collections class As we have seen in previous videos in the collection class We have a sort method and we can pass first We have to convert this entire has set into a list and that a list We have to pass it here in the collection dot sort method and then it will able to sort and it will able to shuffle But we cannot directly sort and shuffle the objects or elements in the has set Okay so these are all different methods are available in the has set and when we need to prefer has it if you have if you want to or maintain elements in the random order where the duplicates are not allowed then we can go for asset concept okay now we'll see practically how we can use all the methods okay right so now now let's move on to the demo part okay now we'll see how we can work with the asset methods so here i already created java collections project which contains collection demo package now inside this i'm just creating a new class i'll name it as hash set asset demo1 let's take main method and then say finish so first of all let's see how many ways we can create an object of has set the first way is commonly we do like has set and here s is a uppercase data has set and let's create one variable called HS equal to new has set so this is a way we can create now we can import has set from Java dot util this is a a variable which we have created for a asset class and this is a default asset object and the default capacity is the default capacity so the default capacity is 16 and what is the load factor here the default load factor will be 0.75 okay now similarly if i want to create this is our default capacity 16 and load factor is 0.75 now suppose i want to configure this load factor and the default capacity then what i can do is i can define like this has set hs equal to new has set and here i can specify 100 so 100 is representing capacity so initial capacity you can say initial capacity is 100 so which we can configure it and suppose along with the initial capacity i just want to configure load factor also then i can specify like this i say has set hs equal to new has set and here along with the locations so for example i want to just store 100 elements let's say 100 and comma and we need to specify the load factor suppose i want to specify 90 percent as a 0.90 and this we have to typecast with a float okay so this is a syntax you have to use and here the initial capacity is 100 and load factor is 0.90 so 90 percent and this is one more way we create we can create has set object suppose i want to restrict the type of objects like i want to store similar kind of objects in my has set then i can create like this i'll say has set hs is my reference variable equal to new has set and here we have to specify the type of the elements like what kind of object it is we have to specify suppose i want to store only numbers i'll say integer wrapper class here and also here I can specify like this. Okay, so these are the different ways we can create has set objects. Now once we created has set objects now we'll see how we can add elements how we can remove the elements how we can read the elements or object from the has set. Now let me just use the first syntax which is more popular and very familiar and let me just comment the rest of them. Okay, now let me just take the first one. so this is my default capacity 16 and load factor is 75 okay now so once we created has set object we can access all the methods which are present inside the has set so how we can add elements into has set now we'll see that add objects or elements elements into has set okay how to add objects or element into asset so simply we can use add method sh dot add off so it will by default accept heterogeneous data so i can pass different type of data so i'll say 100 similarly i can also add more number of elements so here i'm just adding some other value let's say welcome and here i will add a decimal number let's say some decimal number and here i will add some character and here I will add some boolean value which is true and then here I'll add this dot add I can add null also I can add so nulls also accepted okay but duplicates not allowed okay so this is a way we can add all the objects into has it so once I added all the elements now I want to print them I can say system dot out dot println I can specify the has set object so that will print all the elements now this is a very important point so in which order we have inserted all the elements so this is the exact order we followed but has set will not maintain any order so insertion order is not preserved now when i print this object let us see in which order these elements will be printed so when i run as a java application now observe so in which order it is printed so first null is printed then a 100 16.4 welcome and then true so these are the values or objects aligned in random order so insertion order is not preserved so sometimes after some time if you run the same code again this order will be different so that means insertion order will not be preserved so that's the first important point next one so here insertion order not preserved not preserved okay and now we'll try to remove object or element from the has set so how to remove it so to remove it we have a method available called hs has set object dot remove so here we don't have an index concept so what we can do is we can just directly specify the value so i want to just remove 16.4 so that i can directly specify like this 16.4 will be removed So after removing this, so here the 16.4 is a value, not an index. So it will not accept any index here. So after removing the value, let us try to print this again. So I'll say system.out.println. I can simply say sh after removing the element, after removing element or object. So I'm just again printing the. has set elements now let us execute so now we got these elements so this time 16.4 is removed and rest of the elements are printed in the random model so here we can use a remove method along with the value or object we can specify now suppose now i want to get a specific value from this asset directly we cannot get so here we don't have a get method okay So if you want to read all the data, we have to write a loop statement and then we can read each and every object from this has set. Now similarly, we cannot also modify the existing value here. Okay, so we don't have set method to update the existing value. So here after removing we can verify whether that object is element is present in this has set or not. So for that we have another method called contains. So this method will check. the object or element is present in this has it or not. So for example, let's say HS dot HS dot contains method I can use and say HS dot contains and here contains and directly we specify the value. So suppose I want to check welcome is a value or object which is present or not. I can pass this. So this will return either true or false if this element or object is present in the has set, which will return true or else it will return false. Now I'm printing that value and say system dot out dot println, hs dot contains and welcome. So this is how we can check whether this value is present in the has set or not. Similarly, if I want to get some other value, let's say x, y, z I'm giving so this is a not there in the has set. So that should return false. So when I run this the first one written false because this element is not present. So why it is not present because here I say w and here it is a lowercase letter. So there's a reason the first one also written the false. So it is a case sensitive. So now I have changed this. When I run this the first one is returns to because that element or object is present inside the has set. The second one is written false so that it is not present. So contains method we can use like this and suppose I want to check this has. set is empty or not so simply i can say hs dot is empty hs dot is empty so this will return true if it is empty and if it is written false if it is not empty so currently our asset is not empty so it will return false i'll say system dot out dot println hs dot is empty so this should return false now let us run this code so which will return false so basically it will check our asset is empty or not okay so now we'll see how we can read all the objects or element from this has set so to read all these objects or element from the has set we can use either for each loop or we can use iterator method so these are only two ways we have to use to read the data or elements or objects from the has set so the first method i'm using reading objects or elements from has set has set using for each loop okay for each loop now how we can do that i'll say for and here i'll specify the hash map object element let's say has set hs and i'm going to read each and every data element into one variable so here i'll create one variable called object type variable so that this variable will able to hold all kinds of data and from this object or variable i am getting the value so i can directly print this i can simply say system dot out dot println and specify the value so this variable so every round of iteration the from the hs it will get you one element in this object variable then it is printed here so let us run this code so when i run as a java application so which will print all the data from the has set again it is in random order you will not expect in the same order because it will not maintain the insertion order so this is the first way we can get all the objects or element from the has set so apart from this we can also use iterator method to get the data so for that we have to create one iterator class object let's iterator let's say id equal to new id equal to hs is my hs is my asset object so dot iterator method we have to access iterator. So when I call this method from the HS it will get all elements into it. So iterator we have to import from java.util. Now from this it we have to extract each and every value. So for that I can say while loop and here I will just specify if it.has next element and then I will print that element. I can say system. out.println and here i can say okay system.out.println here i'll say it.next so this method will verify it will just print the existing element on this and then it will move on to the next element so this loop will repeat multiple times till it printed all the objects are element from this has set now let us execute now right click run as java application Now here we got the same data. So this is how we can also use iterator to read all the data element from the has set. Now let me show you a few more things on the has set. Let me just go back and let me discuss few more methods. Let's create another class. I'll say has set demo2. So here. Let me just show you few more methods. Let's say we have add all and remove all methods. Add all and remove all methods. so how we can use those methods in the has set so let me just create one has set like this i'll say hash set has set and hs or we can simply say some variable name i'm creating okay so i'll say even number even number equal to new hash set okay new has it so in this even number has set is this object of has set and I want to store only numbers in this only integer numbers then I can explicitly specify the type of the object integer here and also here integer so now has set should be imported from Java dot util so now I created one variable which can hold data elements now I want to add few elements in the even number so here I want to add a few elements how to add even number object dot add and here I can specify some numbers I'm going to add a few numbers into this object I'll say 2 is a even number 4 is another even number 6 is another even number so I'm just added three elements into this has set now I'm going to print them I can say system dot how dot print Ellen even number so this is my object reference variable so this is my even number okay so here i got so i'm just saying has set has set and just printing that message and then followed by numbers so when i run this which will give you all elements so two four six so now it is given in inserting order but every time don't expect it should be uh followed in the insertion order so two four six is given so this is printed two four six so now i have just created one hash map variable and i have added three elements into hashback now even number contains a three different elements even number contains a three different elements now i'm creating one more has set has set one more has set and this name of the asset is numbers equal to new has set and this is also i'm in this also i'm going to store only numbers i'll say integer and here also and specify the integer this is one more asset so we already created one asset with even number which contains the three data elements now i am created one more asset called numbers so inside this asset also i'm just going to add few more elements so what are the elements i'm going to add in this is whatever elements we already added into into the even number has it the same elements i'm going to add into the new asset okay so how we can add so just i want to add this even number has set object into this one so by using add all method so now you can use like this numbers dot you are adding multiple data elements so we have to use add all and here we have to specify the collection so what collection we have already created has it is a one collection we created which contains the multiple elements so we can specify directly here Now what this method will do is this will add whatever data elements we have in this has set even number has set the same number of elements will be added into the new has set. And apart from this, I will also add one more element into this like I'll say numbers dot add and here I'll add 10. Now observe. So numbers is a new has set which we have created and I'm adding all even number. has set so whatever elements we have in the even number asset will be added to the numbers along with that i'm also added one additional element which is 10. now let us print all the elements which are available in the numbers so for that i can say system dot out dot print ln here i will say new has set new has set yes i am printing the numbers has set here okay so along with this collection i am also added one more element so what is our expected output here is two four six along with the ten so this these are the elements we get it from here so this is how we can use add all method so all this i am just showing you to demonstrate add all method okay and similarly we also have one more method called remove all method remove all method so how we can use this remove all method so from this number so currently what are the values we have in these numbers has a hash set we have four values two four six and ten so i want to remove group of elements that is two four six okay and 10 should be there 2 4 6 i want to just remove so how we can remove them numbers dot numbers dot i'll say remove all remove all so when i say remove all group of elements or collection we have to specify so 2 4 6 is present inside the even number collection so that i am going to specify here and now what the statement will do is this will remove all method will remove the data element from the even number Whatever the data elements we have in the even number the same data elements will be removed from numbers Okay. Now I am going to print the rest of the elements system dot out dot println I'll just print the numbers. Okay. This is a has set numbers and printing So here previously has it contains the four elements two four six ten now I removed all even number elements and then it will remove all the even number elements and whatever the rest of elements we have in the numbers that will be printed here so remove all method we can use to remove group of elements so now we can see only 10 is left and rest of the elements are removed so this is how we can use add all and remove all methods so apart from this we can also do some more actions on the sets normally in the mathematics we do so whenever a set we can into union of two sets. I have a two sets are defined. I want to do union or I want to find a common elements between two sets like intersection or I want to find the difference between two sets. So how we can do that? So we have a certain a few more methods are available in the has set to perform a union intersection and differences. Now let us see how we can use those methods. So here I'm just creating one more class and he'll say has set Demo 3, let's take a main method and then finish. So here let me just demonstrate how we can perform union of sets, how we can perform intersection of set, how we can perform the difference, like how we can find the difference between two sets. So union, intersection and difference. So we don't have union all because if I say union all duplicates element will be there. So has set will not allow you duplicate elements. so we have only union intersection and difference now we'll see how we can perform these operations by using different method from the asset so before that let me create one asset which contains the number of elements so i'll say has set and set equal to new has set and here i want to store only numbers in this has set so i can just explicitly specify integer type and the same thing i will specify here now import this asset from java.util now i want to add some elements into the set so let me just name it as a set one because if you have a two sets then i can perform these operations now set one i'll say set one dot add so i'm just adding a new element let's say one similarly let me add a few more numbers one this is two this is three four and five so five elements i have added one two five numbers i have added into the asset now let me just print the set one system dot out dot and tell n and i'm just printing the set one has set one and the elements are present inside the set one okay now i have created one asset the name of the asset is set one which contains five elements Now similarly I will also create one more set then I can perform union intersection and difference. So I will create one more has set and this I'll call it as a set two as a new has set and this is also having same kind of data which is integer so I'll just say integer class here and here. So this will allow us to store only numbers. So in the set two I'm adding one more element let's add three and similarly i will also add few more numbers i'll say three four and five so these are the three elements which we have inside the set two so now again i'm trying to print set two also here and has set two set two and now execute so now i have just created two sets set one and set two set one contains these elements these are the elements we have in the set one you and these are the elements we have in the set two okay so now let us see how we can perform the union So what is union means it will collect all the elements from the set one and set two and it preserve only unique elements it will remove all the duplicates. Okay, so it will find out a unique elements from the set one and set two. So for that what you can do is we have to use a method called add all observe this. So from the set one dot add all okay add all of set two. Now what this method will do is Whatever the elements we have in the set 2, all those elements will be added to the set 1. If there are any duplicates, it will remove those duplicates and it will preserve only unique elements. Now, when I print set 1 here, this will give you union results. I will say system.out.println and here I will say union and I will print set 1. And this will give you... elements which are matching with the union so 1 2 3 4 5 so these are the elements we have in the set 1 so 1 2 3 4 5 this is a result of union for both the set 1 and set 2 and similarly we can also do intersection intersection means the common elements between two sets in set 1 and set 2 whatever the common elements we have we can get it so that is called intersection the common elements we can get from the set 1 and set 2 So for that we have to use one more method. Let me comment these two. Now for that I can use set one dot. We have a method called retain all. I'll say set one dot retain all. So this is a method. So here I'll say set two. Here I'll say set two. So what exactly this method will do retain all means retain all means except the set two. Except the set two elements, rest of the elements will be retained. Rest of the elements will be retained except the set two. So that will give you the common elements intersection part of set one and set two. So what are the common elements we have in the set one and set two? Only three, four, five are the common elements which we have in the both. So by using retain all method we can get it. So after that, let us try to print set one. System.out.println and here I'll print intersection elements. so here i can say set one okay now this will give you the common elements between two sets now you can see three four five are the common elements between two sets so by using return on method we can get the common elements and suppose i want to find the difference so set one and say two what are the difference elements we have in the set one and set two that is basically called as a difference now how we can find the difference of sets to set one and set two. So simply here we can say from the set one, remove all of set two. So what this method will do is from the set one elements from the 12345, it will remove all set two elements in the set to what are there three, four, five are there. So 345 will be removed from the set one and what are the rest of elements one two are there. So these one two are the element they are the difference between set one and set two. So here I will print the difference. I'll say system.out.println and here I can say difference and I'll print set one okay and then I'll comment this. So this will give you the difference between set one and set two right. So now we got one comma two. So this is how we can get the difference between two sets and we can also get the subset of the set. So how we can get the subset? Okay, subset how we can get the subset. So here I can say, let me comment these two statements. And here I can say, set one dot, there is a method called contains all set one dot contains all I'll say set to what exactly this method will do is contains all will determine the true or false. Okay, so contains all set to so whatever the elements we have in the set to they are three four five okay and those elements are available in the set one okay if whatever elements we have in the set two so the same elements are available in the set one that that will be written true okay so now let us try to print this set one contains all basically so we can say system dot out dot println which will give you the subset elements basically system.out.println here I can say subset subset of set one all right so subset it will be so what is the subset here means So set one contains 12345 set two contains a 345 and apart from these 345 if these 345 contains in the set one also okay and what is the other elements we have that will be written that is a subset of the set one. So here contains all set two will verify in set one and other than those elements it will be written rest of the elements. So set two contains 345 set one is also contains 345. other than those three elements what are other rest of elements we have will be written as a subset so this command will return the subset of the element from the set one so that will contains all so let me okay so one two three four five okay perfect let me just painted okay i commented everything so here set one contains all set two now i have printed set one perfect now let me save it and execute. So this is given subset 12345. Okay, so basically this command contains all will return either true or false Boolean value will be written if the set to having three values 345 and set one is also having 345 along with 123 right. So these elements are already contains in the set one. So this command will exactly returns true or false. So let us try to print here. system.out.println and set1.containsAll of 2. So this will return either true or false. And if you want to print subset like this, we can say set1. So set1 is basically having 1, 2, 3, 4, 5. So it is printing like this. So let me just comment this and run this command. So containsAll method will return the set2 elements are present in the set1. Then it will return true. If that means if the set2 elements are part of the subset of set1. So the set two elements are subset of set one, then it will return true, or else it will return false. So when I run this as a Java application, now it is written true. So that means basically what it is verifying the set to either subset of set one or not it is very fine. So whatever elements we have in the set two, they are also having in the set to set one. So the same elements are having in the set one also, so it is returning true. So basically, it is verifying whether the set 2 is a subset of set 1 or not if it is subset then it will returns true or else it didn't false okay so we can just remove this statement so this is how we can use or we can do some kind of union interaction or intersection and difference and subset operations on the asset okay so this is how we can use all the methods on asset collection framework in java okay so that's all for this video guys so thanks for watching