What is the difference between them? I know that
A LinkedHashSet is an ordered version of HashSet that
maintains a doubly-linked List across all elements. Use this class instead of HashSet
when you care about the iteration order. When you iterate through a HashSet the
order is unpredictable, while a LinkedHashSet lets you iterate through the elements
in the order in which they were inserted.
But in sourcecode of LinkedHashSet there are only calling constructors of HashSet. So where is double-linked List and insertion order?
Best Answer
The answer lies in which constructors the
LinkedHashSet
uses to construct the base class:And (one example of) a
HashSet
constructor that takes a boolean argument is described, and looks like this: