LinkedHashSet Example in Java

LinkedHashSet extends the HashSet and is the implementation of Hash Table and Linked List. LinkedHashSet maintains a doubly-linked list through all of its elements. This helps in maintaining the insertion-order of the elements. Since LinkedHashSet extends the HashSet, they have all the basic properties of the HashSet.

Important Points to Rememeber about LinkedHashSet
1. Order of Insertion is maintained by the LinkedHashSet
2. Even if you try to reinsert the same element, the insertion order is not affected.
3. Null value is accepted by the LinkedHashSet

Lets see LinkedHashSet Example in Java.

LinkedHashSet Example in Java

In this example we will see
1. How to Create LinkedHashSet
2. Add Elements to it.
3. Add null Value to LinkedHashSet
4. Add the same value again to the LinkedHashSet and see if its allowed.


When you print the linkedHashSet, you will see that the Null value is accepted by the Set. But similar to HashSet the same value added again is not accepted. Neither is the order of insertion affected by the same value inserted again.