This post will discuss the difference between HashSet, TreeSet, and LinkedHashSet classes in Java.

A Set is a collection in Java that does not allow any duplicate entries. There are different types of sets that have different features and performance. HashSet, TreeSet and LinkedHashSet are the most commonly used implementations of the Set interface, which happens to be more or less similar in functionality. This post provides an overview of some of the major differences between these implementations.

1. Overview of HashSet

A HashSet is a class that implements the Set interfaces and uses a hash table as its underlying data structure to store elements. A hash table is a data structure that maps keys to values using a hash function. In a HashSet, each element is considered as a key that maps to itself as a value and the elements can be of any type. A HashSet offers many advantages over other types of sets:

  • HashSet allows fast access to elements by using the hash code of the elements.
  • HashSet does not maintain any order of the elements in the set, unlike TreeSet or LinkedHashSet.
  • HashSet does not allow duplicate elements in the set, unlike a List.
  • HashSet allows null elements in the set, unlike a TreeSet.

HashSet provides several methods to manipulate the set, such as add(), remove(), contains(), etc. These methods allow us to add, delete, or check elements in the set. Here is an example of how to create and use an HashSet in Java:

Download  Run Code

2. Overview of TreeSet

A TreeSet is a class that implements the Set and NavigableSet interfaces, and uses a red-black tree as its underlying data structure to store elements. A red-black tree is a self-balancing binary search tree that maintains an ordered sequence of elements. The elements in a TreeSet are sorted by default in their natural ordering or by a provided Comparator. A TreeSet offers some advantages over other types of sets:

  • TreeSet allows fast access to elements as it uses a self-balancing tree structure for storing the elements.
  • TreeSet maintains the order of the elements in the set, unlike a HashSet.
  • TreeSet does not allow duplicate elements in the set, unlike a List.
  • TreeSet does not allow null elements in the set, unlike a HashSet or LinkedHashSet.

TreeSet provides several methods to manipulate the set, such as add(), remove(), contains(), etc. These methods allow us to add, delete, or check elements in the set. Moreover, TreeSet also provides additional methods to navigate the set in a sorted order, such as first(), last(), lower(), higher(), etc. These methods allow us to access the first or last element in the set, or find an element that is less than or greater than a given element. For example:

Download  Run Code

3. Overview of LinkedHashSet

A LinkedHashSet is another class that implements the Set interface and uses a hash table and a linked list as its underlying data structure to store elements. A hash table is a data structure that maps keys to values using a hash function. A linked list is a data structure that stores elements as nodes that are connected by pointers. In a LinkedHashSet, each element is considered as a key that maps to itself as a value, where elements can be of any type. A LinkedHashSet offers some advantages over other types of sets:

  • LinkedHashSet allows fast access to elements by using the hash code of the elements.
  • LinkedHashSet maintains the order of the elements in the set, unlike a HashSet.
  • LinkedHashSet does not allow duplicate elements in the set, unlike a List.
  • LinkedHashSet allows null elements in the set, unlike a TreeSet.

LinkedHashSet provides several methods to manipulate the set, such as add(), remove(), contains(), etc. However, unlike TreeSet, it does not provide any additional methods to navigate the set in a sorted order. For example:

Download  Run Code

4. Difference between HashSet, TreeSet, and LinkedHashSet

Here are some of the main differences between HashSet, TreeSet, and LinkedHashSet in Java:

1. Implementation Details

A HashSet implementation is backed by a hash table using a HashMap instance. A TreeSet implementation is based on a TreeMap implemented as red-black tree, and LinkedHashSet is implemented using a hash table and doubly linked list. Both HashSet and LinkedHashSet classes implements the Set interface, whereas TreeSet implements the NavigableSet interface.

HashSet, TreeSet, and LinkedHashSet provides utility methods to manipulate the set, such as add, remove, contains, etc. A TreeSet provides some more methods to manipulate and navigate the set, such as first, last, lower, higher, etc.

2. Iteration Order

The most important difference between the HashSet, TreeSet, and LinkedHashSet class lies in the order in which its iterator returns contents of the set.

  1. HashSet makes no guarantees on the iteration order of the set or even the order will remain constant over time.
  2. TreeSet, depending on the constructor used, is iterated according to the natural ordering of its elements or according to the specified Comparator.
  3. LinkedHashSet runs a doubly linked list through all of its elements, which defines the predictable iteration order that is the same as the order in which elements were inserted into the set.

3. Performance

Both HashSet and LinkedHashSet allows fast access to elements by using the hash code of the elements. They offers O(1) time performance for the basic operations such as add, remove, contains and size, etc. This assumes that the hash function uniformly distributes elements in the bucket. However, due to the added expense of maintaining the doubly linked list, LinkedHashSet performance is slightly lower than that of HashSet. TreeSet is slowest among all which guarantees O(log(n)) time cost for these operations since it is implemented using a red-black tree.

Also, HashSet requires less memory than TreeSet and LinkedHashSet since it uses a hash table to store its elements. LinkedHashSet has the extra overhead of a doubly linked list, and since TreeSet is implemented as a red-black tree, which takes a considerable amount of memory.

4. Null values

Both HashSet and LinkedHashSet permit a null values in the set, whereas TreeSet does not permit null values in the set by default. A TreeSet can support null only if its Comparator supports comparison on null, which is not the case with its default order.

5. Which Implementation to use?

Here are some general recommendations on how to choose between HashSet, TreeSet, and LinkedHashSet in Java:

  • If you need to store a collection of unique values in a performance critical application, we can use HashSet.
  • If you need to store a collection of unique values but also maintain the order of the elements according to their natural ordering or by a Comparator, we should use TreeSet.
  • If you need to store a collection of unique values that maintain the insertion order of the elements, we should use LinkedHashSet.
  • If you need to store a collection of unique values that also support multithreading, we should use ConcurrentSkipListSet instead. This class will ensure that the set is synchronized and thread-safe and prevent any data inconsistency or concurrency issues.

That’s all about the differences between HashSet, TreeSet, and LinkedHashSet in Java.