What is TreeSet in java?

It is the implementation of SortedSet interface.This implementation provides guaranteed log(n) time cost for the basic operations (add, remove and contains). The class is not synchronized.
Similar to HashSet, but one advantage:
  • While elements are added with no regard for order, they are returned (via iterator) in sorted order.
What is sorted order?
  • this is defined either by having class implement Comparable interface, or passing a Comparator object to the TreeSet Constructor. 
  • Latter is more flexible: doesn’t lock in specific sorting rule, for example. Collection could be sorted in one place by name, another by age, etc.


Enter your email address to get our daily JOBS & INTERVIEW FAQ's Straight to your Inbox.

Make sure to activate your subscription by clicking on the activation link sent to your email