What is the difference between HashSet and TreeSet?

Technology CommunityCategory: JavaWhat is the difference between HashSet and TreeSet?
VietMX Staff asked 3 years ago

The HashSet is Implemented using a hash table and thus, its elements are not ordered. The add, remove, and contains methods of a HashSet have constant time complexity O(1). On the other hand, a TreeSet is implemented using a tree structure. The elements in a TreeSet are sorted, and thus, the add, remove, and contains methods have time complexity of O(logn).