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. Unlike a regular Set, a SortedSet guarantees that its elements will be iterated in ascending order according to their natural ordering or the ordering specified by a comparator.

  • 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

That’s all about the SortedSet Interface in java. If you have any queries or feedback, please write us email at Enjoy learning, Enjoy Java.!!

SortedSet Interface
Scroll to top