Hello everyone welcome you all in the today's video we are going to learn about linked asset concept. So in our previous video we have seen like we have a collection interface which is a root interface of all collections and which is again having one more child interface called as a set interface this is also interface and the set interface is implemented by using two different classes one is we have seen has set class So this is the one class we have seen in yesterday's class and there is one more class we have called linked Linked has set. So this is one more class which is implemented set interface So set is a class set set is an interface which is a child interface of collection and has set and linked has set So these are the two classes which have implemented all methods from the set interface So today we are going to discuss about linked has set.
Okay linked has set So here let us try to understand what is the basic difference between a has set and a linked has set and why we have a two implemented classes. So when I talk about has set and a linked has set, when I say has set this is a class which we have discussed and today we are going to discuss about linked has set. So when I talk about these two So these two classes almost the same so whatever methods have implemented inside the hash set the same number of methods have implemented inside the Linked hash set but what is the basic difference between these two are in the hash sets duplicates are not allowed Okay, duplicates elements or objects are not allowed and the same way we have we can also insertion order Let's say insertion order insertion order not preserved so that means the elements or objects will be stored in the random order so when you come to the linked hash map here also duplicates not allowed so duplicates not allowed but insertion order is preserved so here insertion order so insertion order is preserved so there's a basic these are only the basic difference between hash map and a linked hash map so whatever methods are implemented in hash set class the same methods are implemented in the linked hash set so here in the hash set duplicates are not allowed insertion order is also not preserved in case of linked hash set duplicates are not allowed but insertion order is preserved so the underlying data structure of hash set is hash table so the data structure internally will follow to store the elements and in case of linked hash set it is also having has table concept these are data structure plus linked list data structure so these are the combination of data structures will be followed in the linked has set so here has table plus linked list so that is the reason linked has set will also internally having linked list concept so whatever elements we are inserting in the linked has set those elements will be inserted in the in the same order so in whichever way we have inserted elements or objects the same order the elements will be stored inside the linked has set so that's the only difference between has set and a linked has set so in has set duplicates are not allowed in case of linked has set also duplicates are not allowed this is a similar criteria but here insertion order is not preserved whereas linked has set insertion order is preserved in whichever way we have inserted elements or object in the same way the elements will be maintained in the linked has set so this is the only difference between hashSet and linked hashSet and when I create a hashSet object or linked hashSet object. So here again initial size initial size is 16 locations will be allocated and also load factor is also same. So load factor the default load factor is 0.71.
So here also in the linked hashSet also we can configure the initial size and as well as load factor. So these are the similarities between hashSet and linked hashSet classes both have implemented already. the same interface called as a set interface so additionally in the linked list in the linked asset the underlying data structure will be hash table plus linked list so that's the only difference between asset and a linked asset and whatever methods are available in the asset the same methods are implemented inside the linked asset also okay so this is the basic understanding so now we will see a small demo on linked asset okay now let's move on to the demo okay now we'll see how we can work with a linked has set class so i have a java project which is created in my eclipse and i also have one package inside the project now inside the package i'm just creating a new class and i will name it as linked has set linked has set demo and let's take a main method and then say finish so now i created a new class in my project and so as a first step let me just declare a has a linked asset variable i'll say has set and which is linked has it we are discussing so linked has it and i'll say lset equal to new link asset and this is how we can just create a variable so you can just import linked asset from java.util package and here also and if you want to restrict this has set with some data type or specific to object or if you want to store a similar type of object you can just specify that so here in the angular brackets i want to store only integers then i can say integer class so i can just import this integer this is a wrapper class which we have to import and similarly here also we can just specify that so this is a way we can declare linked has set a variable or if you want to store different type of objects simply you can say linked has set link it has it and you can directly specify the object name equal to new link has set so you can directly specify that if you do if you want to store a different type of objects okay so this is linked has set right so now this is my link it has set variable which you have created now let us add some objects or elements into the link it has that variable so how we can add lset.
so this is my reference variable of linked asset and dot we can use add method so whatever methods we have discussed in has set the same methods will work here also so lcd.add i'm adding some numbers here so i'm just going to add some values let's say 100 say 200 and 300 and 400 and i'll say 500 so i'm just storing five uh different values and of the same type so because it is a linked has set so insertion order is preserved duplicates not allowed like in hazard also duplicates are not allowed and here also duplicates not allowed but insertion order is preserved here so that is the basic difference between half set and a linked asset so before doing this let me also create an object of has set okay so we'll see the different behavior between these two so has set l set equal to new has it so this is the way we can create an object of asset right so let me use this one first so now lset is a just object of asset so i have added a few elements and just i am trying to print them so i'll say system dot dot println i'll say lset so this is currently pointing to the has set class okay now what is the behavior of asset the behavior of asset is duplicates are not allowed insertion order is also not preserved so instance not is not preserved means when i print these elements it should print in the random order it will not be same in the insertion order so when i just run as java application so now we can see so these elements will be printed in the different order so we can just look at here and whichever way we have inserted it is not printed in the exactly the same way so this is a behavior of asset now if i just change this variable with the linked asset now this variable is referring linked hazard cons so in case of linked has set and the same elements have inserted now we'll see the difference now when i print this so this will print in the same order so this is the difference between has set and the linked has set okay so in has set the insertion order is not preserved so that elements are displayed in the random order in case of linked has set insertion order is preserved so we got the elements in the same order so in whichever order we have inserted in the same order we got so apart from this whatever methods we have already discussed in the has set concept the same methods are applicable for linked headset also so add add all remove remove all contains contains all so all these methods are exactly the same way it will work for linked asset also okay so this is a small topic of linked asset collection in java okay so that's all for this video guys thanks for watching