Sunday, August 15, 2021

HashSet Vs LinkedHashSet Vs TreeSet in Java

Though HashSet, LinkedHashSet and TreeSet all are implementation of the Set interface and share some traits like storing only the unique elements, not being thread-safe but there are certain differences too related to performance, how elements are ordered etc. So it is very important to know these differences among HashSet, LinkedHashSet and TreeSet in Java as it will help you to make an informed choice which Set implementation to use to meet the requirement.

HashSet Vs LinkedHashSet Vs TreeSet in Java

  1. First and most important difference is related to the ordering of the elements.
    HashSet is unordered, it will store elements on the basis of the calculated hash that's it.
    LinkedHashSet maintains the insertion ordering of the elements.
    TreeSet keeps the element in sorted order. The sorting orders TreeSet follows by default is the natural ordering of the elements.

  2. Another difference is related to allowing null value.
    HashSet as well as LinkedHashSet allow null value to be stored. Mind you only one null value is allowed in both HashSet and LinkedHashSet.
    TreeSet does not allow null value, trying to add null to a TreeSet will result in a null pointer exception.

  3. For HashSet and LinkedHashSet comparison of the elements is done using equals() method. Note that set allows only unique elements, and that uniqueness is maintained by using the equals() method to compare elements.
    TreeSet does the comparison of the element using the compareTo (or compare) method, depending on whether sorting is done using Comparable or Comparator.
  4. Performance wise HashSet is at the top as it has no added baggage of insertion ordering or sorting. If hashing is done correctly HashSet offers constant time performance O(1) for the basic operations (add, remove, contains and size). As I mentioned this performance is assuming that the hash function disperses the elements properly among the buckets. If HashCode is not proper the performance may degrade and in worst case it will be O(n).
    For LinkedHashSet performance is likely to be just slightly below that of HashSet, due to the added expense of maintaining the linked list.
    TreeSet has to sort elements with every insertion so that way performance is slow, but TreeSet provides guaranteed log(n) time cost for the basic operations (add, remove and contains) irrespective of the number of elements stored.

That's all for this topic HashSet Vs LinkedHashSet Vs TreeSet 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. EnumSet in Java With Examples
  3. How to Sort Elements in Different Order in TreeSet
  4. Difference Between ArrayList And LinkedList in Java
  5. Java Collections Interview Questions And Answers

You may also like-

  1. Difference Between Comparable and Comparator in Java
  2. Java ReentrantLock With Examples
  3. Lambda expression examples in Java 8
  4. Spliterator in Java
  5. finalize method in Java
  6. super Keyword in Java With Examples
  7. Multi-Catch Statement in Java Exception Handling
  8. Why wait(), notify() and notifyAll() must be called inside a synchronized method or block?

1 comment:

  1. Very Good Explanation...thanks a lot for providing such a good site...

    ReplyDelete