|
Answer» The HashSet class implements the Set interface, supported by a Hashtable. Due to the implementation of the Set Interface, duplicate VALUES are not ALLOWED. The underlying data structure for a HashSet is a Hashtable. The TREESET class implements the SortedSet interface, which in turn extends the Set interface. Objects are stored in a sorted ascending order. The underlying data structure for TreeSet is a tree. Here are a few notable differences between a HashSet and a TreeSet: Basis
| HashSet
| TreeSet
|
|---|
Performance
| HashSet in Java offers faster performance than TreeSet
| TreeSet in Java is slower than HashSet for most of the generic operations like add, remove and search.
| Underlying data structure
| HashSet has hashtable as its underlying data structure.
| TreeSet has a red black tree as its underlying data structure.
| NULL element
| HashSet does allow one null element
| TreeSet doesn’t allow any null element to be present
| Implementation
| It is implemented by HashMap
| It is implemented by TreeMap
| Interface
| HashSet implements the Set interface.
| TreeSet implements the SortedSet interface. SortedSet interface extends the Set interface.
| Sorting
| HashSet is not in sorted order
| TreeSet is sorted in ascending order
| COMPARISON
| HashSet applies the equals() method for comparison
| TreeSet applies the compareTo() method for comparison.
|
|