Java LinkedHashSet

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

Java LinkedHashSet class

Java HashSet class hierarchy

Java LinkedHashSet class is a Hash table and Linked list implementation of the set interface. It inherits HashSet class and implements Set interface.

The important points about Java LinkedHashSet class are:

  • Contains unique elements only like HashSet.

  • Provides all optional set operations, and permits null elements.

  • 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 extends HashSet implements Set, Cloneable, Serializable  


Constructors of Java LinkedHashSet class




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 linkedhashset 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.

Example of LinkedHashSet class:

  1. import java.util.*;  
  2. class TestCollection10{  
  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(;  
  12.   }  
  13.  }  
  14. }  

Test it Now


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. = id;  
  8. = name;  
  9. = 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.publisher+" "+b.quantity);  
  28.     }  
  29. }  
  30. }  


101 Let us C Yashwant Kanetkar BPB 8
102 Data Communications & Networking Forouzan Mc Graw Hill 4
103 Operating System Galvin Wiley 6