TreeSet in Java
TreeSet is the concrete implementation of NavigableSet, SortedSet and Set interfaces. Treeset is the collection which sorts the elements and maintains that order. TreeSet implements SortedSet, NavigableSet. Any collection implementation that implements sorted set should sort the elements when they are getting added to the collection.
That means when we fetch the elements from collection they are already sorted.
TreeSet by default sorts the elements based on natural order ie., The sorting criteria defined in the compareTo method of a type.
A custom comparator can also be specified with the TreeSet for Sorting.