Compare the Difference Between Similar Terms

Difference Between TreeSet and HashSet

Key Difference – TreeSet vs HashSet
 

Most programming languages support Arrays. It is a data structure that is used to store multiple elements of the same data type. If there is array declared for six elements, then it cannot be used to store ten elements. Therefore, arrays are not dynamic and cannot change the size of the array once it is declared. Programming languages such as Java supports Collections that are used to store data dynamically. Collections support operations such as adding elements and deleting elements. There is a number of interfaces and classes in the collection hierarchy. The base interface is the Collection interface. Set is an interface that extends the Collection interface. It does not allow duplication. The TreeSet and HashSet are two class in the Collection hierarchy and both implements the Set interface. TreeSet is a class that implements the Set interface and it is used to store unique elements in ascending order.  HashSet is a class that implements the Set interface and it is used to store unique elements using the Hashing mechanism. The key difference between TreeSet and HashSet is that TreeSet stores the elements in ascending order while the HashSet does not store the elements in ascending order. Both TreeSet and HashSet only store unique elements.

CONTENTS

1. Overview and Key Difference
2. What is a TreeSet
3. What is a HashSet
4. Similarities Between TreeSet and HashSet
5. Side by Side Comparison – TreeSet vs HashSet in Tabular Form
6. Summary

What is a TreeSet?

TreeSet class implements NavigableSet interface. The NavigableSet interface extends SortedSet, Set, Collection and Iterable interfaces in hierarchical order. TreeSet always maintains the ascending order. If the elements were inserted in B, A, C order, they will be stored as A, B, C. The methods such as add (), remove ()can be used with TreeSet object. The add method can be used to add an element. The remove method is used to remove an element from the collection. Those are some methods that can be used with TreeSet.

Figure 01:  Program with TreeSet

According to the above program, an object of type TreeSet is created. The string data elements are added to that object using the add method. The data inserted order is A, D, A, B, C, D. Using the iterator, the stored values are printed to the screen. The output is A, B, C, D. Even though, there are two A letters and two D letters, the output displays the one A and one D each. Therefore, the TreeSet stores unique elements. There is no particular insertion order but when observing the output, it can be seen that the TreeSet maintains the ascending order of the elements.

What is a HashSet?

The HashSet class extends AbstractSet class that implements Set Interface. The Set interface inherits Collection and Iterable interfaces in hierarchical order. In HashSet, there is no guarantee that the elements will maintain the ascending order and the inserted order. If the inserted order was A, B, C then the values might store as C, A, B. Storing order can also be A, B, C but there is no guarantee that the inserted order or ascending order is maintained.

Figure 02: Program with HashSet

According to the above program, an object of type HashSet is created. The string data elements are added to that object using the add method. The data inserted order is L, R, M, M, R, L. Using the iterator, the stored values are printed to the screen. The output is R L M. Even though, there are two L, R and M letters from each, only one letter from each is displayed. Therefore, the HashSet stores unique elements. When observing the output, it can be seen that there is no ascending order or the inserted order is maintained.

What are the Similarities Between TreeSet and HashSet?

What is the Difference Between TreeSet and HashSet?

TreeSet vs HashSet

TreeSet is a class in the collection hierarchy that is used to store unique elements in ascending order. HashSet is a class in the collection hierarchy that is used to store unique elements using the Hashing mechanism.
 Element Storing
TreeSet stores the elements in ascending order. HashSet does not store the elements in ascending order.

Summary – TreeSet vs HashSet

In programming, it is required to store data elements dynamically. Programming languages such as Java supports Collections to achieve this task. There is a number of interfaces and classes in the collection hierarchy. The TreeSet and HashSet are two class in the Collection hierarchy. Both implements the Set interface. TreeSet is a class that implements the Set interface and it is used to store unique elements in ascending order.  HashSet is a class that implements the Set interface and it is used to store unique elements using the Hashing mechanism. The difference between TreeSet and HashSet is that TreeSet stores the elements in ascending order while the HashSet does not store the elements in ascending order. This article discussed the difference between TreeSet and HashSet.

Reference:

1.“TreeSet in Java – javatpoint.” JavaPoint. Available here
2.“HashSet in Java – javatpoint.” JavaPoint  Available here