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:-
|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:
- HashSet():Â This constructor is used to create a default HashSet.
- HashSet(Collection C):Â Used in initializing the HashSet with the elements of the collection C
- LinkedHashSet(int size):Â Used to initialize the size of the LinkedHashSet with the integer mentioned in the parameter.
- 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