As you already know, Linkedhashset are an ordered version of HashSet. That is means, HashSet doesn ' t maintain any order where as Linkedhashset maintains insertion order of the elements. Linkedhashset uses doubly linked list internally to maintain the insertion order of it ' s elements. We have seen this on how Linkedhashset Works internally in Java?. As Linkedhashset maintains doubly linked list (along with HashMap), the performance of linkedhashset is slightly slower th The HashSet. But, linkedhashset'll be very useful if you need a collection of elements placed in the order they has inserted. We'll see one such example of linkedhashset in this article.
Let's consider the want to create a pool of customers placed in the order they has arrived. Assume that it's also mandatory that duplicate customers must isn't be allowed. For such requirements, Linkedhashset are the best suitable. In this article, we'll try to implement this example using Linkedhashset class.
Understand:
Let's create Customer class with the Fields–name and ID.
classcustomer{String name; intID; PublicCustomer (String name,intID) { This. Name =name; This. ID =ID; } @Override Public inthashcode () {returnID; } @Override Publicboolean equals (Object obj) {Customer Customer=(Customer) obj; return(id = =customer.id); } @Override PublicString toString () {returnId+" : "+name; }}
You might has observed that equals () and Hashcode () methods in the above class is overrided so, Customer objects wil L is compared solely based on ID. That means the objects had same ID would be considered as duplicates and they would not be allowed in the pool.
Create one Linkedhashset object containing elements of Customer type.
Set New Linkedhashset<customer> ();
Add Some elements to this set.
Set. Add (NewCustomer ("Jack",021)); Set. Add (NewCustomer ("Peter", the)); Set. Add (NewCustomer ("Ramesh",415)); Set. Add (NewCustomer ("Julian",814)); Set. Add (NewCustomer ("Avinash", the));//Duplicate Element Set. Add (NewCustomer ("Sapna",879)); Set. Add (NewCustomer ("John",546)); Set. Add (NewCustomer ("Moni",254)); Set. Add (NewCustomer ("Ravi", the));//Duplicate Element
Iterate through this linkedhashset.
Set while (It.hasnext ()) { = (Customer) It.next (); System. out . println (customer);}
Output would be,
- : Jack the : Peter 415 : Ramesh 814 : Julian 879 : Sapna 546 : John 254 : Moni
You can notice that Customer objects is placed in the order they is inserted into the set and also duplicate elements ar E avoided.
Below is the code for the whole program.
classcustomer{String name; intID; PublicCustomer (String name,intID) { This. Name =name; This. ID =ID; } @Override Public inthashcode () {returnID; } @Override Publicboolean equals (Object obj) {Customer Customer=(Customer) obj; return(id = =customer.id); } @Override PublicString toString () {returnId+" : "+name; }} Public classmainclass{ Public Static voidMain (string[] args) {//Creating LinkedhashsetLinkedhashset<Customer>Set=NewLinkedhashset<customer>(); //Adding elements to Linkedhashset Set. Add (NewCustomer ("Jack",021)); Set. Add (NewCustomer ("Peter", the)); Set. Add (NewCustomer ("Ramesh",415)); Set. Add (NewCustomer ("Julian",814)); Set. Add (NewCustomer ("Avinash", the));//Duplicate Element Set. Add (NewCustomer ("Sapna",879)); Set. Add (NewCustomer ("John",546)); Set. Add (NewCustomer ("Moni",254)); Set. Add (NewCustomer ("Ravi", the));//Duplicate Element//Getting Iterator ObjectIterator<Customer> it =Set. iterator (); while(It.hasnext ()) {Customer Customer=(Customer) it.next (); System. out. println (customer); } }}
The study of Linkedhashset