English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Java Basic Tutorial

Java Flow Control

Java Arrays

Java Object-Oriented (I)

Java Object-Oriented (II)

Java Object-Oriented (III)

Java Exception Handling

Java List (List)

Java Queue (Queue)

Java Map Collection

Java Set Collection

Java Input/Output (I/O)

Java Reader/Writer

Other Java Topics

Java SortedSet Interface

In this tutorial, we will learn about the Java SortedSet interface and its methods through an example.

Java Collections framework's SortedSet interface is used to store elements in a certain order in the collection.

It inherits fromSet interface.

Classes that implement SortedSet

To use the functions of the SortedSet interface, we need to use the TreeSet class that implements it.

How to use SortedSet?

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

//Implementation of SortedSet through TreeSet class
SortedSet<String> animals = new TreeSet<>();

We created a sorted set using the TreeSet class.

Here, we have not used any parameters to create the sorted set. Therefore, the set will be naturally sorted.

Methods of SortedSet

The SortedSet interface includesThe Set interface includesAll methods. This is because Set is the super interface of SortedSet.

In addition to the methods included in the Set interface, the SortedSet interface also includes the following methods:

  • comparator() - Return a comparator that can be used to sort elements in the collection

  • first() - Return the first element of the collection

  • last() - Return the last element of the collection

  • headSet(element) - Return all elements before the specified element

  • tailSet(element) - Return all elements in the collection after the specified element (including the specified element)

  • subSet(element1,element2) - Return element1and element2all elements between, including element1


Implementation of SortedSet in TreeSet class

import java.util.SortedSet;
import java.util.TreeSet;
class Main {
    public static void main(String[] args) {
        //Create SortedSet using TreeSet
        SortedSet<Integer> numbers = new TreeSet<>();
        //Insert element into the set collection
        numbers.add(1);
        numbers.add(2);
        numbers.add(3);
        numbers.add(4);
        System.out.println("SortedSet: " + numbers);
        //Access element
        int firstNumber = numbers.first();
        System.out.println("First Number: " + firstNumber);
        int lastNumber = numbers.last();
        System.out.println("Last Number: " + lastNumber);
        //Delete Element
        boolean result = numbers.remove(2);
        System.out.println("Numbers ",2Was it deleted? " + result);
    }
}

Output Result

SortedSet: [1, 2, 3, 4]
First Number: 1
Last Number: 4
Numbers2Was it deleted? true

To learn more about TreeSet, please visitJava TreeSet.

Since we have known the SortedSet interface, we will use the TreeSet class to learn its implementation.