Difference between HashSet and LinkedHashSet?

Difference between HashSet and LinkedHashSet?

Difference between HashSet and LinkedHashSet?

Difference between HashSet and LinkedHashSet?
  • It is the child class of HashSet.
  • A LinkedHashSet is an ordered version of HashSet that maintains a doubly-linked List across all elements.
  • Contains unique elements only like HashSet.
  • Provides all optional set operations, and permits null elements.
  • Maintains insertion order.
  • LinkedHashSet doesn’t have its own methods. All methods are inherited from its superclass i.e HashSet. So. all operations on LinkedHashSet work in the same manner as that of HashSet.
  • It is exactly same as HashSet( including constructors and methods) except the following difference:-
No HashSet LinkedHashSet
1 The underlying data structure is Hashtable. The underlying data structure is a combination of  LinkedList and Hashtable.
2 Insertion order is not preserved. Insertion order is preserved.
3 Introduced in 1.2 version Introduced in 1.4 version.

 

Below is the list of constructors supported by the LinkedHashSet:

  1. HashSet(): This constructor is used to create a default HashSet.
  2. HashSet(Collection C): Used in initializing the HashSet with the elements of the collection C
  3. LinkedHashSet(int size): Used to initialize the size of the LinkedHashSet with the integer mentioned in the parameter.
  4. LinkedHashSet(int capacity, float fillRatio): Can be used to initialize both the capacity and the fill ratio, also called the load capacity of the LinkedHashSet with the arguments mentioned in the parameter. When the number of elements exceeds the capacity of the hash set is multiplied by the fill ratio thus expanding the capacity of the LinkedHashSet

Leave a Reply

Your email address will not be published. Required fields are marked *

*