LinkedHashSet in Java

Posted on Updated on

LinkedHashSet extends HashSet class. So, all methods available with HashSet is available here in LinkedHashSet. LinkedHashSet maintains a linked list of the entries in the set, in the order in which they were inserted. This allows insertion-order iteration over the set. Following are the features of LinkedHashSet class.

  1. It uses hash table internally for storage.
  2. As it implements Set interface, doesn’t allow duplicate entries.
  3. Insertion order of elements are maintained. while in case of HashSet insertion order doesn’t maintained.

Similar to HashSet, LinkedHashSet provides following constructors.

Construtor Description
LinkedHashSet ()  Default construtor
LinkedHashSet (Collection c)  Creates object of LinkedHashSet using elements of collection c.
LinkedHashSet (int capacity) Creates object of LinkedHashSet with initial capacity.
LinkedHashSet (int capacity, float fillRatio) Creates object of LinkedHashSet with initial capacity and fillRatio. The fill ratio must be between 0.0 and 1.0.

Example of LinkedHashSet


[obj1, obj2, obj3]

You can see in output that the insertion order of elements are maintained in LinkedHashSet which is not the case with HashSet.

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.