Friday, 25 September 2015

LinkedHashSet in Java

LinkedHashSet is also one of the implementation of the Set interface. Actually LinkedHashSet class extends the HashSet and has no other methods of its own.

Just like other implementations of the Set interface HashSet and TreeSet, LinkedHashSet also stores unique elements. How LinkedHashSet differs from the HashSet is that it maintains the insertion-order; that is elements in the LinkedHashSet are stored in the sequence in which they are inserted. Note that insertion order is not affected if an element is re-inserted into the set.

How it is implemented

LinkedHashSet is the Hash table and linked list implementation of the Set interface, with predictable iteration order. This implementation differs from HashSet in that it maintains a doubly-linked list running through all of its entries. If you have idea about how HashSet works internally in Java, you must be knowing internally HashSet uses HashMap for storing its elements.
In case of LinkedHashSet it will call the methods of HashSet only for all the operations but it will use LinkedHashMap internally.

Just take any of the Constructor of the LinkedHashSet and you will see all those contructors will ultimatey call the Constructor of the HashSet where map will be initialized as a LinkedHashMap.

LinkedHashSet Example code

public class LinkedHSDemo {

    public static void main(String[] args) {
        // Using Diamond operator Which is available from Java 7
        // Use LinkedHashSet<String> if using less than version 7
        Set<String> citySet = new LinkedHashSet<>();
        // Iterating the Set
        for(String str : citySet){
            System.out.println("" + str);





Points to note here -

  • It can be seen that the insertion order is maintained. While iterating the LinkedHashSet elements are displayed in the order they were inserted.
  • Even though Delhi is inserted twice it is displayed only once which shows that the insertion order is not affected if an element is re-inserted into the set.
  • Only one null is allowed in the LinkedHashSet, even if I have tried to insert null twice it can be seen that only one is stored.

LinkedHashSet is not synchronized

LinkedHashSet is not thread safe. In case we need to Synchronize it, it should be synchronized externally. That can be done using the Collections.synchronizedSet method.

Set s = Collections.synchronizedSet(new LinkedHashSet(...));

LinkedHashSet class's iterator is fail-fast

The iterators returned by this class's iterator method are fail-fast: if the set is modified at any time after the iterator is created, in any way except through the iterator's own remove method, the iterator will throw a ConcurrentModificationException.

Performance of LinkedHashSet

The performance of LinkedHashSet is likely to be just slightly below that of HashSet, due to the added expense of maintaining the linked list. But there is one exception: Iteration over a LinkedHashSet requires time proportional to the size of the set, regardless of its capacity. Iteration over a HashSet is likely to be more expensive, requiring time proportional to its capacity as in the case of HashSet based on the Hashing function it may have to go through all the buckets .

That's all for this topic LinkedHashSet in Java. If you have any doubt or any suggestions to make please drop a comment. Thanks!

Related Topics

  1. How HashSet works internally in Java
  2. TreeSet in Java
  3. EnumSet in Java
  4. Overriding hashCode() and equals() method in Java
  5. How Linked List class works internally in Java
  6. Java Collections interview questions

You may also like -

No comments:

Post a Comment