Java tutorials for Beginners – Java SortedSet Interface

(Java programming Example for Beginners)

Java SortedSet Interface

In this tutorial, we will learn about the SortedSet interface in Java and its methods with the help of an example.

The SortedSet interface of the Java Collections framework is used to store elements with some order in a set.

It extends the Set interface.

The Java SortedSet interface extends the Set interface.

Class that implements SortedSet

In order to use the functionalities of the SortedSet interface, we need to use the TreeSet class that implements it.

The Java TreeSet class implements the SortedSet interface.

How to use SortedSet?

To use SortedSet, we must import the java.util.SortedSet package first.

// SortedSet implementation by TreeSet class
SortedSet<String> animals = new TreeSet<>();

We have created a sorted set called animals using the TreeSet class.

Methods of SortedSet

The SortedSet interface includes all the methods of the Set interface. It’s because Set is a super interface of SortedSet.

Besides methods included in the Set interface, the SortedSet interface also includes these methods:

  • comparator() – returns a comparator that can be used to order elements in the set
  • first() – returns the first element of the set
  • last() – returns the last element of the set
  • headSet(element) – returns all the elements of the set before the specified element
  • tailSet(element) – returns all the elements of the set after the specified element including the specified element
  • subSet(element1, element2) – returns all the elements between the element1 and element2 including element1


Implementation of SortedSet in TreeSet Class

import java.util.SortedSet;
import java.util.TreeSet;

class Main{

    public static void main(String[] args){
        // Creating SortedSet using the TreeSet
        SortedSet<Integer> numbers = new TreeSet<>();

        // Insert elements to the set
        System.out.println("SortedSet: " + numbers);

        // Access the element
        int firstNumber = numbers.first();
        System.out.println("First Number: " + firstNumber);

        int lastNumber = numbers.last();
        System.out.println("Last Number: " + lastNumber);

        // Remove elements
        boolean result = numbers.remove(2);
        System.out.println("Is the number 2 removed? " + result);


SortedSet: [1, 2, 3, 4]
First Number: 1
Last Number: 4
Is the number 2 removed? true



Java tutorials for Beginners – Java SortedSet Interface

Sign up to get end-to-end “Learn By Coding” example.

Two Machine Learning Fields

There are two sides to machine learning:

  • Practical Machine Learning:This is about querying databases, cleaning data, writing scripts to transform data and gluing algorithm and libraries together and writing custom code to squeeze reliable answers from data to satisfy difficult and ill defined questions. It’s the mess of reality.
  • Theoretical Machine Learning: This is about math and abstraction and idealized scenarios and limits and beauty and informing what is possible. It is a whole lot neater and cleaner and removed from the mess of reality.
Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.