LinkedHashSet in Java

By |2017-05-22T04:58:32+05:30May 18th, 2015|java|

In the previous tutorials we have discuss about what is HashSet and Tree Set with customizable sorting. Now we are going to discuss about LinkedHashSet.

What is LinkedHashSet ?

LinkedHashSet is child class of HashSet, which was introduced at Java 1.4 Version. The underlying data structure is the combination of HashTable and LinkedList, hence it is termed as LinkedHashSet then it doesn’t allow the duplicates, and it will maintain the insertion order.

LinkedHashSet = HashTable+LinkedList


The main difference between the HashSet and Linked Hash Set is, the Linked Hash Set maintain the insertion order, where HashSet doesn’t maintain insertion order.

Example Of LinkedHashSet

public class LinkedHashSetDemo {
    public static void main(String[] args) {
        LinkedHashSet lhs = new LinkedHashSet();
        System.out.println("Elements : "+lhs);

[box type=”success” align=”alignleft” class=”” width=”100%”]

Elements : [P, null, A, p, 0, z]


On the above example, we can see the output with insertion order.

When we should go with LinkedHashSet

If we want to store a group of objects in a collection, where duplicates are not allowed and insertion order is maintain, then we go with the Linked Hash Set.

Key points

  1. The underlying data structure for Linked Hash Set is HashTable and LinkedList.
  2. In Linked Hash Set duplicate elements are not allowed.
  3. While iterating the Linked HashSet,we can expect the elements, according to our insertion order.
  4. Linked Hash Set allows the heterogeneous elements.

Happy Learning 🙂

About the Author:

Hi Folks, you have reach this so far, that shows you like what you are learning. Then why don't you support us to improve for bettor tutorials by leaving your valuable comments and why not you keep in touch with us for latest updates on your favorite blog @ facebook , twitter , Or Google+ ,

Leave A Comment