All three classes implement the Set interface and offer mostly the same functionality. The most important difference is the order in which iteration through the elements will happen:
HashSet makes absolutely not guarantees about the iteration order. It can (and will) even change completely when new elements are added.
TreeSet will iterate according to the "natural ordering" of the elements according to their compareTo()method (or an externally supplied Comparator). Additionally, it implements the SortedSet and NavigableSet interfaces.
HashSet makes absolutely not guarantees about the iteration order. It can (and will) even change completely when new elements are added.
TreeSet will iterate according to the "natural ordering" of the elements according to their compareTo()method (or an externally supplied Comparator). Additionally, it implements the SortedSet and NavigableSet interfaces.
SortedSet
provides a total ordering on its elements.
The elements are ordered using their natural
ordering, or by a Comparator
typically provided at sorted
set creation time. The set's iterator will traverse the set in
ascending element order.
Several additional operations (first,last,subSet,headSet and tailSet) are provided
to take advantage of the ordering.