LinkedHashSet in Java – Set With Order

LinkedHashSet in Java is also one of the implementation of the Set interface like HashSet and Treeset. How LinkedHashSet differs from the other implementation
HashSet
is that unlike HashSet which is unordered, LinkedHashSet is an ordered collection, it maintains iteration ordering, which is the order in which elements were inserted into the set.

LinkedHashSet implementation in Java

LinkedHashSet maintains a doubly-linked list running through all of its entries. This linked list defines the iteration ordering. LinkedHashSet class in Java extends HashSet apart from implementing Set interface and uses the methods of the HashSet class itself.

If you have idea about the HashSet internal implementation then you must be knowing that internally HashSet uses HashMap to store its element. Same way LinkedHashSet internally uses LinkedHashMap.

Features of LinkedHashSet in Java

Some of the features of the LinkedHashSet which are discussed in this post are as follows-

  1. LinkedHashSet in Java stores only unique elements.
  2. LinkedHashSet is an ordered collection and maintains the insertion order.
  3. LinkedHashSet permits null elements.
  4. LinkedHashSet is not thread-safe.
  5. The iterators returned by the iterator method of the LinkedHashSet class are fail-fast. Which means, 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 throws a ConcurrentModificationException.

Java LinkedHashSet constructors

LinkedHashSet class in Java has 4 constructors.

  • LinkedHashSet()– Constructs a new, empty linked hash set with the default initial capacity (16) and load factor (0.75).
  • LinkedHashSet(int initialCapacity)– Constructs a new, empty linked hash set with the specified initial capacity and the default load factor (0.75).
  • LinkedHashSet(int initialCapacity, float loadFactor)– Constructs a new, empty linked hash set with the specified initial capacity and load factor.
  • LinkedHashSet(Collection<? extends E> c)– Constructs a new linked hash set with the same elements as the specified collection.

Java example creating a LinkedHashSet

This example shows how LinkedHashSet is created and elements added to it.

Output

As you can see from the output insertion order is maintained. Also duplicate element is added only once and it doesn’t affect the insertion order, same way null is added only once even if it is added more than once.

LinkedHashSet implementation is not synchronized

LinkedHashSet in Java is not thread safe as it is not synchronized. If multiple threads access a LinkedHashSet concurrently, and at least one of the threads modifies the set structurally, it must be synchronized externally. You can wrap your LinkedHashSet using the Collections.synchronizedSet method.

Java LinkedHashSet iterator

The iterators returned by LinkedHashSet’s collection view methods 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 throws a ConcurrentModificationException.
Iterating LinkedHashSet Java example

Output

Performance of LinkedHashSet

Like HashSet, LinkedHashSet provides constant-time performance for the basic operations (add, contains and remove), assuming the hash function disperses elements properly among the buckets. Though performance wise LinkedHashSet is likely to be just slightly below that of HashSet, due to the added expense of maintaining the linked list. One exception is iteration that is faster in LinkedHashSet because of the linked list implementation.

Reference: https://docs.oracle.com/javase/10/docs/api/java/util/LinkedHashSet.html

That’s all for the topic LinkedHashSet in Java – Set With Order. If something is missing or you have something to share about the topic please write a comment.


You may also like

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.