A LinkedHashSet can be termed as a HashTable and LinkedList implementation of the Set interface, with a predictable iteration order. The difference between a HashSet and a LinkedHashSet is that the latter uses a doubly-linked list to maintain all of its entries. This link maintains the order of iteration of the elements, in the order in which they are inserted into the Set. And The insertion order doesn’t change if an already existing element is re-inserted into the set.
- LinkedHashSet maintains a doubly-linked List across all elements.
- It maintains insertion order
- It extends HashSet class and implements Set interface.
- Except insertion maintains, it inherit remaining all properties from HashSet
Constructors in LinkedHashSet:
- LinkedHashSet(): This constructor is used to create a default HashSet.
- LinkedHashSet(Collection C): Used in initializing the HashSet with the eleements of the collection C
- LinkedHashSet(int size): Used to intialize 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 with the fill ratio thus expanding the capacity of the LinkedHashSet
Example:
Output:
true
true
false