Introduction to Java LinkedHashSet With Examples

Welcome to the Introduction to Java LinkedHashSet collection. We will go through the major features, behavior and examples of LinkedHashSet.

Introduction

The LinkedHasHSet is an implementation of a Set Collection in Java. The Set collections are known for storing no-duplicates. The LinkedHashSet is a modified version of Java HashSet. Hence LinkedHashSet extends HashSet. The HashSet is a unique, unordered, and unsorted implementation of the Set collection.

It internally uses Hash Table and Linked List. Hence it provides a predictable iteration order. In other words, it iterates the records in the order of insertion. Unlike HashSet, the LinkedHashSet maintains a double linked list to store the records.

The Set Collections in Java are Unique. Hence, being a Set implementation, LinkedHashSets are also a unique collections. Also, It allows you to insert one and only one null value.

Features of LinkedHashSet

Let’s quickly go over major featues of LinkedHashSet.

  • LinkedHashSet is an implementation of Set Collections and stores unique data.
  • The order of insertion is preserved. Hence elements are returned in the same order.
  • TreeSets are Sorted Collections. However LinkedHashSets are not.
  • Can contain one and only one null element.
  • The elements are stored in a form of double linked list.
  • The add, remove and contains operations are constant in time.
  • The operations are not synchronised. Therefore you need to handle the synchronization on your own.
  • The Iterator is fail-fast. In other words if the set is modifed once the iterator is open, iterator throws an exception (ConcurrentModificationException).

Examples of LinkedHashSet

LinkedHashSets have following Constructors.

  • LinkedHashSet(): Default constructor that creates an empty mutable LinkedHashSet of size 16 and load factor of 0.75.
  • LinkedHashSet(Collection c): Creats a LinkedHashSet containing all the unique elements in the given collection.
  • LinkedHashSet(int capacity): Creates a LinkedHashSet of the given initial capacity. The load factor is still at default of 0.75
  • LinkedHashSet(int capacity, float loadFactor): Creates an empty LinkedHashSet of the given initial capacity and load factor.

Let’s create our fist LinkedHashSet and see how it behaves.

Set<String> s = new LinkedHashSet<>();

s.add("a");
s.add("s");
s.add("s");
s.add(null);
s.add("d");
s.add("b");
s.add(null);Code language: Java (java)

When we print it:

s.forEach(System.out::println);

// Output
// a
// s
// null
// d
// bCode language: Java (java)
  • It ignored the second “s”.
  • Also, preserved the order of insertion.
  • No sorting.
  • Also, the second null is ignored.

Multiple Threads and LinkedHashSet

As we discussed earlier the operations on LinkedHashSets are not synchronized. In other words, multiple threads can access the same instance at the same time. When you create an Iterator over a LinkedHashSet, consider it like a snapshot of the current state. Hence, if another thread at the same time modifies the state of set, the iterator throws ConcurrentModificationException. This is a safety feature of the Set collections. Therefore, you won’t get dirty reads with the Sets.

However, you can still have a synchronized version of the LinkedHashSet. For example, see the below code:

Set<String> linkedHashSet = new LinkedHashSet<>();

Set<String> synchronizedSet = 
        Collections.synchronizedSet(linkedHashSet);Code language: Java (java)

The synchronizedSet is a synchronised LinkedHashSet.

When to Use LinkedHashSet

LinkedHashSets are a combinations of an ArrayList and a HashSet. Because they preserve order like an ArrayList and they allow unique elements like HashSet.

Because of this you can use them in many scenarios where you want to deduplicate records without changing their sequence. For example, you want to extract list of website visitors. Where, soem users might have visited multiple times. Also, you want to maintain the order in which they visited the website.

If you use LinkedHashSet or any collection for that matter, you should first know the data and also the way you want to use that data. In other words, if you don’t bother about the oder or sorting of data and just concerned about uniqueness, you should use HashSet. HashSets are faster than LinkedHashSet. Because, they don’t have to maintain an extra layer of Double Linked List.

Similarly, if you want records to be maintained in certain order but don’t want Sorting then LinkedHashSet is best. Because, TreeSets have to mantain extra overheads of tree structure.

Summary

This is the end of this short Introduction of LinkedHashSets. To sum up, we learnt that LinkedHashSets are implementation of Set and are support ordering. They preserve records in the order of insertions. Like HashSets they do not allow duplicates and allow only one null element.

Moreover, LinkedHashSets are backed by a double linked list and hence are expensive compared to HashSets. Because of the underlying Hash Table the operations like add, remove and contains are constant in time, irrespective of the size of the set.