SortedSet Interface

SortedSet Interface

In this tutorial, we are going to discuss SortedSet class in java. The SortedSet interface present in the java.util package extends the Set interface present in the collection framework.

  • It is child interface of Set.
  • If we want to represent a group of “unique objects” where duplicates are not allowed and all objects must be inserted according to some sorting order, then we should go for the SortedSet interface.
  • That sorting order can be either default natural sorting (or) customized sorting order.
  • SortedSet interface define the following 6 specific methods.

1. Object first();

2. Object last();

3. SortedSet headSet(Object obj);

Returns the SortedSet whose elements are <obj.

4. SortedSet tailSet(Object obj);

It returns the SortedSet whose elements are >=obj.

5. SortedSet subset(Object o1,Object o2);

Returns the SortedSet whose elements are >=o1 but <o2.

6. Comparator comparator();

Returns the Comparator object that describes underlying sorting technique. If we are following default natural sorting order then this method returns null.

SortedSet Interface
SortedSet Interface

Scroll to top