Sunday, September 15, 2019

Java LinkedHashSet class

Java LinkedHashSet class is a Hashtable and Linked list implementation of the set interface. It inherits HashSet class and implements Set interface.
The important points about Java LinkedHashSet class are:
  • Java LinkedHashSet class contains unique elements only like HashSet.
  • Java LinkedHashSet class provides all optional set operation and permits null elements.
  • Java LinkedHashSet class is non synchronized.
  • Java LinkedHashSet class maintains insertion order.

Hierarchy of LinkedHashSet class

The LinkedHashSet class extends HashSet class which implements Set interface. The Set interface inherits Collection and Iterable interfaces in hierarchical order.




LinkedHashSet class declaration

Let's see the declaration for java.util.LinkedHashSet class.
  1. public class LinkedHashSet<E> extends HashSet<E> implements Set<E>, Cloneable, Serializable  

Constructors of Java LinkedHashSet class

ConstructorDescription
HashSet()It is used to construct a default HashSet.
HashSet(Collection c)It is used to initialize the hash set by using the elements of the collection c.
LinkedHashSet(int capacity)It is used initialize the capacity of the linked hash set to the given integer value capacity.
LinkedHashSet(int capacity, float fillRatio)It is used to initialize both the capacity and the fill ratio (also called load capacity) of the hash set from its argument.

Java LinkedHashSet Example

Let's see a simple example of Java LinkedHashSet class. Here you can notice that the elements iterate in insertion order.
  1. import java.util.*;  
  2. class LinkedHashSet1{  
  3.  public static void main(String args[]){  
  4.  //Creating HashSet and adding elements  
  5.         LinkedHashSet<String> set=new LinkedHashSet();  
  6.                set.add("One");    
  7.                set.add("Two");    
  8.                set.add("Three");   
  9.                set.add("Four");  
  10.                set.add("Five");  
  11.                Iterator<String> i=set.iterator();  
  12.                while(i.hasNext())  
  13.                {  
  14.                System.out.println(i.next());  
  15.                }  
  16.  }  
  17. }  
One
Two
Three
Four
Five

Java LinkedHashSet example ignoring duplicate Elements

  1. import java.util.*;  
  2. class LinkedHashSet2{  
  3.  public static void main(String args[]){  
  4.   LinkedHashSet<String> al=new LinkedHashSet<String>();  
  5.   al.add("Ravi");  
  6.   al.add("Vijay");  
  7.   al.add("Ravi");  
  8.   al.add("Ajay");  
  9.   Iterator<String> itr=al.iterator();  
  10.   while(itr.hasNext()){  
  11.    System.out.println(itr.next());  
  12.   }  
  13.  }  
  14. }  
       Ravi
       Vijay
       Ajay

Java LinkedHashSet Example: Book

  1. import java.util.*;  
  2. class Book {  
  3. int id;  
  4. String name,author,publisher;  
  5. int quantity;  
  6. public Book(int id, String name, String author, String publisher, int quantity) {  
  7.     this.id = id;  
  8.     this.name = name;  
  9.     this.author = author;  
  10.     this.publisher = publisher;  
  11.     this.quantity = quantity;  
  12. }  
  13. }  
  14. public class LinkedHashSetExample {  
  15. public static void main(String[] args) {  
  16.     LinkedHashSet<Book> hs=new LinkedHashSet<Book>();  
  17.     //Creating Books  
  18.     Book b1=new Book(101,"Let us C","Yashwant Kanetkar","BPB",8);  
  19.     Book b2=new Book(102,"Data Communications & Networking","Forouzan","Mc Graw Hill",4);  
  20.     Book b3=new Book(103,"Operating System","Galvin","Wiley",6);  
  21.     //Adding Books to hash table  
  22.     hs.add(b1);  
  23.     hs.add(b2);  
  24.     hs.add(b3);  
  25.     //Traversing hash table  
  26.     for(Book b:hs){  
  27.     System.out.println(b.id+" "+b.name+" "+b.author+" "+b.publisher+" "+b.quantity);  
  28.     }  
  29. }  
  30. }  
Output:
101 Let us C Yashwant Kanetkar BPB 8
102 Data Communications & Networking Forouzan Mc Graw Hill 4
103 Operating System Galvin Wiley 6


No comments:

Post a Comment

How to DROP SEQUENCE in Oracle?

  Oracle  DROP SEQUENCE   overview The  DROP SEQUENCE  the statement allows you to remove a sequence from the database. Here is the basic sy...