Java SortedSet<E> InterfaceLast Updated : 14 Jan 2026 The SortedSet<E> interface in Java is part of the Java Collections Framework and provides a collection of unique elements, where the elements are stored in sorted order. It extends the Set<E> interface. It was introduced in Java 2 and has been an essential part of the Java programming language ever since. A set is used to provide a particular ordering on its element. The elements are ordered either by using a natural ordering or by using a Comparator. All the elements which are inserted into a sorted set must implement the Comparable interface. The SortedSet<E> interface is a subtype of the Set<E> interface, which means that it inherits all of its methods and adds additional functionality related to sorting. Being a set, it does not allow duplicate elements, and its elements are maintained in sorted order. The set's iterator will traverse the set in an ascending order. Several other operations are provided in order to make best use of ordering. All the elements must be mutually comparable. SortedSet Interface Methods
Natural Ordering and Custom ComparatorsThe natural ordering of elements in a SortedSet<E> is determined by the implementation of the Comparable interface by the elements themselves. If the elements do not implement Comparable, a ClassCastException will be thrown at runtime. Alternatively, we can specify a custom comparator by providing an instance of the Comparator interface when creating the SortedSet<E>. It allows sorting based on criteria other than the natural ordering of elements. Implementations of SortedSet<E>In Java, the TreeSet<E> class is the most common implementation of the SortedSet<E> interface. It uses a Red-Black tree data structure to store elements in sorted order, providing guaranteed log(n) time cost for the basic operations like add, remove, and contains. Use CasesThe SortedSet<E> interface is useful in scenarios where we need to maintain a collection of unique elements in sorted order. Some common use cases include:
Performance Considerations
Example: SortedSetExample.java Output: Sorted Set: [apple, banana, orange] First element: apple Last element: orange Subset: [apple, banana] Example 1: JavaSortedSetExample1.java Output: The list of elements is given as: Audi BMW Baleno Mercedes The first element is given as: Audi The last element is given as: Mercedes The respective element is given as: [Audi, BMW] The respective element is given as: [Audi, BMW, Baleno, Mercedes] Example2: SortedSetExample.java Output: Sorted Set: [2, 3, 5, 8, 10] First element: 2 Last element: 10 Head set (less than 5): [2, 3] Tail set (greater than or equal to 5): [5, 8, 10] Subset (from 3 inclusive to 8 exclusive): [3, 5] Comparator used: null Next TopicWhat is Framework in Java |
We request you to subscribe our newsletter for upcoming updates.

We deliver comprehensive tutorials, interview question-answers, MCQs, study materials on leading programming languages and web technologies like Data Science, MEAN/MERN full stack development, Python, Java, C++, C, HTML, React, Angular, PHP and much more to support your learning and career growth.
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India