Javatpoint Logo
Javatpoint Logo

HashSet vs TreeSet

In Java, the entire Collections Framework is built upon a set of standard interfaces. Several standard implementations (such as LinkedList, HashSet, and TreeSet) of these interfaces are provided that we may use as-is. In this section, first, we will discuss HashSet and TreeSet with proper examples. Also, we will discuss the differences and similarities between HashSet and TreeSet.

HashSet vs TreeSet

The following figure shows the hierarchy of HashSet and TreeSet in the Collections framework.

HashSet vs TreeSet

HashSet

HashSet is a generic class of the Java collection framework. It extends AbstractSet and implements the Set interface. It creates a collection that uses a hash table for storage. The hash table stores the information by using the hashing mechanism.

Hashing uses the informational content to determine a unique value which is known as hash code. It is used as the index in which data is stored that is associated with the key. The transformation of the key into hash code performed automatically. The benefit of hashing is that it allows the execution time of add, contain, remove, and size operation to remain constant even for large sets. Its time complexity for the operation search, insert, and delete is O(1).

The HashSet class does not provide any additional methods. It uses the methods of its superclasses and interfaces. It is to be noted that it does not guarantee the order of its elements.

Let's understand the HashSet through a Java program.

HashSetExample.java

Output

HashSet vs TreeSet

In the above output, we observe two things. The first one is the elements are not sorted in the natural order and the second thing is that the duplicate element Chicago has been removed.

TreeSet

TreeSet is a class of Java collection framework that extends AbstractSet and implements the Set, NavigableSet, and SortedSet interface. It creates a collection that uses a tree for storage.

TreeSet is a generic class of the Java collection framework. It implements the Set interface. It uses TreeMap internally to store the TreeSet elements. By default, it sorts the elements in natural order (ascending order). The order of sorting depends on the Comparator that we have parsed. If no Comparator is parsed, it sorts the elements in the natural order.

Its performance is slow in comparison to HashSet because TreeSet sorts the elements after each insertion and deletion operation.

It uses two methods comaperTo() or compare() to compare the elements. It is to be noted that the implementation of TreeSet is not synchronized. It means that it is not thread-safe. The implementation must be synchronized externally if multiple threads accessing a TreeSet concurrently and a thread try to modify the TreeSet.

It does not allow to store null elements. It throws NullPointerException if we try to insert a null element. It requires more memory than TreeSet because it also maintains the comparator to sort the elements.

Its time complexity for the operation search, insert, and delete is O(log n) which is much higher than HashSet. It uses a self-balancing BST (Red-Black Tree) to implement the TreeSet.

Let's understand the TreeSet through a Java program.

TreeSetExample.java

Output

HashSet vs TreeSet

In the above output, we observe two things. The first one is the elements are sorted in the natural order and the second thing is that the duplicate elements Asus has been deleted.

We have deeply understood the HashSet and TreeSet. Now, we will move to the differences between the two.

Difference Between HashSet and TreeSet

Parameters HashSet TreeSet
Ordering or Sorting It does not provide a guarantee to sort the data. It provides a guarantee to sort the data. The sorting depends on the supplied Comparator.
Null Objects In HashSet, only an element can be null. It does not allow null elements.
Comparison It uses hashCode() or equals() method for comparison. It uses compare() or compareTo() method for comparison.
Performance It is faster than TreeSet. It is slower in comparison to HashSet.
Implementation Internally it uses HashMap to store its elements. Internally it uses TreeMap to store its elements.
Data Structure HashSet is backed up by a hash table. TreeSet is backed up by a Red-black Tree.
Values Stored It allows only heterogeneous value. It allows only homogeneous value.

There are some similarities between HashSet and TreeSet:

  • Both the classes implement the Set interface.
  • They do not allow duplicate values.
  • Both HashSet and TreeSet are not thread-safe.
  • They are not synchronized but if we want to make them synchronize, we can use Collections.synchronizedSet() method.
  • The iterator used in both classes are fail-fast in nature. They throw ConcurrentModificationException if we try to modify an iterator once it is created.
  • Both uses the shallow copy technique to create the clone of their objects by using the clone() method.

Which one is better to use?

Use HashSet if you want elements in sorted order else use HashSet because its performance is fast in comparison to TreeSet.






Youtube For Videos Join Our Youtube Channel: Join Now

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA