This subclass of HashSet is a Set implementation based on a hashtable. It defines no new methods and is used just like a HashSet is used. What is unique about a LinkedHashSet is that in addition to the hashtable data structure, it also uses a doubly-linked list to connect the elements of the set into an internal list in the order in which they were inserted. This means that the Iterator returned by the inherited iterator( ) method always enumerates the elements of the set in the order which they were inserted. By contrast, the elements of a HashSet are enumerated in an order that is essentially random. Note that the iteration order is not affected by reinsertion of set elements. That is, if you attempt to add an element that already exists in the set, the iteration order of the set is not modified. If you delete an element and then reinsert it, the insertion order, and therefore the iteration order, does change. Figure 16-37. java.util.LinkedHashSet<E>public class LinkedHashSet<E> extends HashSet<E> implements Set<E>, Cloneable, Serializable { // Public Constructors public LinkedHashSet ( ); public LinkedHashSet (Collection<? extends E> c ); public LinkedHashSet (int initialCapacity ); public LinkedHashSet (int initialCapacity , float loadFactor ); } |