How do you implement a LinkedHashSet in Java?

Table of Contents

Introduction

In Java, the **LinkedHashSet** class is part of the Java Collections Framework and is an implementation of the Set interface. It combines the unique element storage property of HashSet with the insertion order property of LinkedList. Unlike a HashSet, which does not maintain any order of elements, a LinkedHashSet maintains the order in which elements are added to the set. This makes LinkedHashSet a useful data structure when you need to store unique elements while preserving the order of their insertion.

In this article, we will discuss how to implement a LinkedHashSet in Java, its features, and advantages, and provide practical examples of its usage.

Key Features of LinkedHashSet

1. Insertion Order Preservation

The most notable feature of a LinkedHashSet is that it preserves the insertion order of elements. When you iterate through a LinkedHashSet, the elements are returned in the order in which they were added.

For example:

Output:

This shows that the elements are stored in the order they were added.

2. No Duplicate Elements

Like other implementations of the Set interface (such as HashSet), a LinkedHashSet does not allow duplicate elements. If you attempt to add a duplicate element, it will be ignored.

Example:

Output:

The duplicate "Apple" is ignored, and only the unique elements remain.

3. Performance Characteristics

  • Insertion and Lookup: The time complexity for insertion (add()) and lookup (contains()) is O(1) on average, similar to HashSet. This makes LinkedHashSet efficient for operations that involve adding elements or checking for their existence.
  • Iteration: Iteration over a LinkedHashSet is O(n), where n is the number of elements. Since the LinkedHashSet maintains the order of insertion using a linked list, the iteration process has a slight overhead compared to HashSet, but it still offers efficient performance.

4. Maintains Both Hashing and Linked List Structure

A LinkedHashSet combines the properties of hashing (like HashSet) for efficient element lookups and linked list (like LinkedList) to maintain the order of elements. This dual structure provides the advantages of both fast access and order preservation.

5. Allows Null Elements

Unlike TreeSet, which doesn't allow null, a LinkedHashSet allows null elements, similar to HashSet. However, only one null value can be stored in the set.

Example:

Output:

This demonstrates that null can be stored in a LinkedHashSet, but multiple null values are not allowed.

How to Implement a LinkedHashSet in Java

1. Creating a LinkedHashSet

To create a LinkedHashSet, you simply instantiate the class using its constructor. You can also initialize it with an initial capacity or a custom load factor, although the default constructor will suffice for most cases.

Example:

2. Adding Elements

You can add elements to a LinkedHashSet using the add() method, just like with other sets.

Example:

3. Removing Elements

You can remove an element from a LinkedHashSet using the remove() method.

Example:

4. Iterating over LinkedHashSet

You can iterate over a LinkedHashSet using an Iterator or an enhanced for loop. Since the LinkedHashSet maintains insertion order, the iteration will follow the order in which elements were added.

Example:

When to Use LinkedHashSet

  • When insertion order matters: Use LinkedHashSet when you need to maintain the order in which elements were added to the set.
  • When you need unique elements: If your application requires uniqueness and you also want to preserve the order of elements, LinkedHashSet is the ideal choice.
  • When efficient access is needed: Since LinkedHashSet provides average O(1) time complexity for insertion, lookup, and deletion, it is efficient for operations requiring fast access to elements.

Conclusion

The LinkedHashSet class in Java is a highly useful collection that combines the characteristics of both HashSet and LinkedList. It provides constant-time performance for the basic operations like add(), remove(), and contains(), while also preserving the order in which elements were added. Whether you need to store unique elements while maintaining insertion order or when order of retrieval is important, LinkedHashSet is an excellent choice. With its performance characteristics and ease of use, it is an essential part of the Java Collections Framework.

Similar Questions