Java TreeSet is the most popular implementation of java.util.SortedSet. SortedSet is an interface that extends java.util.Set. Java Sorted Set provides total ordering on its elements.

Java TreeSet


In other words, while iterating the TreeSet, we can expect sorted data. Java TreeSet elements are ordered as per their natural ordering or we can provide a Comparator while creating SortedSet. If we don’t provide specific Comparator during set creation, elements must implement the Comparable to ensure natural ordering.

Java TreeSet Constructors

TreeSet is very popular implementation of SortedSet. As per specification, all sorted set implementation classes should provide 4 types of constructors.

  1. A void (no arguments) constructor: It should create a sorted set which is sorted according to the natural ordering of its elements.
  2. A constructor with an argument of type Comparator: It should create a sorted set which is sorted according to the provided Comparator.
  3. A constructor with an argument of type Collection: It should create a sorted set with elements of provided collection which is sorted according to the natural ordering of elements.
  4. A constructor with an argument of type SortedSet: It should behave as a copy constructor and create a new sorted set with the same elements and the same ordering of provided sorted set.

Unfortunately, interfaces can’t contain constructors. So, there isn’t any way to enforce these recommendations.

Java TreeSet Example

Now let’s create a sorted set using different ways, as mentioned earlier we will look at different examples of java TreeSet.

Output of the above java TreeSet example code will be like below image.


Java TreeSet Comparable

Now, we’ll create a sorted set with objects of Person class. To, provide natural ordering Person class should have implementation of Comparable interface.


Java TreeSet Comparator

To provide different ordering, we need to pass custom comparator implementation while creating sorted set. For instance, let’s sort set according to the id attribute of Person class.

Java Sorted Set Example

We can also create sorted set by passing another collection object or a different sorted set.

In both the cases we get following output:

Java SortedSet Methods

SortedSet certainly gets some extra privileges as compared to Set because of its sorted nature. As you might have already guessed, apart from inherited methods from the Set interface, it also provides a few additional methods.

  1. Comparator<? super E> comparator(): Returns the comparator instance used to order elements in the set. If elements are sorted as per their natural ordering, it returns null.
  2. SortedSet<E> subSet(E fromElement, E toElement): Returns a portion of this set for given range. (fromElement is inclusive whereas toElement is exclusive). Note that it returns a view of the subset. Thus, changes performed on the returned set are reflected in actual set.
  3. SortedSet<E> headSet(E toElement): Returns a view of the portion of this set whose elements are strictly less than toElement.
  4. SortedSet<E> tailSet(E fromElement): Returns a view of the portion of this set whose elements are greater than or equal to fromElement.
  5. E first(): Returns the first element of the set which happens to be lowest element in the set.
  6. E last(): Returns the last element of the set which happens to be highest element in the set.

Java SortedSet Implementation

Let’s explore these methods with an example. We’ll create a sorted set by passing a comparator. Here, comparator() method will return the same comparator:

Now, let’s find subset using subSet(from, to) method. Note that the changes made on subset are reflected on the original set as well.


Simillarly, let’s check out other methods:


That’s all for Java TreeSet or java sorted set.

Reference: API Doc

By admin

Leave a Reply