SortedSet interface in Java with Example | Java SortedSet interface Methods, How to Use, Implementation of SortedSet in TreeSet Class

By going through this Java Tutorial, you can learn all about the SortedSet interface in Java with Example. In fact, it includes entire information on how to use java SortedSet and what are the methods of sortedset interface, etc. Moreover, you can also gain knowledge on java sorted set implementation using TreeSet.

This Tutorial of Java SortedSet Contains: 

Java SortedSet Interface

SoretedSet in Java is an interface that is present in java.util package. It is a part of the collection framework in Java. The SortedSet interface extends the Set interface.

SortedSet doesn’t allow you to store duplicate elements. If you want to represent a group of individual objects according to some sorting order where duplicate elements are not allowed, then we should go for SortedSet in Java. All the elements which we want to insert in SortedSet must implement the Comparable interface.

SortedSet interface in Java with Example

Do Check:

Class that implements SortedSet

To make use of the SortedSet Interface Functionalities, we must use the TreeSet Class. Hence, the Class that implements SortedSet in Java is TreeSet.

TreeSet implements SortedSet Image

How to use SortedSet?

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

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

In the above syntax, we have taken the animals sorted set using the TreeSet Class. And here we have used no arguments to create a sorted set. So the set will be sorted directly.

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
numbers.add(1);
numbers.add(2);
numbers.add(3);
numbers.add(4);
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);
}
}

Output:

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

Now that we know about the SortedSet interface, we will learn about its implementation using the TreeSet class.

Creating TreeSet SortedSet Implementation with Comparator

Actually, this comparator decides the ordering of the elements in the TreeSet. By java.util.Comparator implementation to the constructor of the TreeSet, it is possible to pass a Comparator. Below, we have provided how to create a java SortedSet with a Comparator using TreeSet

Comparator comparator = new MyComparatorImpl();

SortedSet sortedSet = new TreeSet(comparator);

Methods of SortedSet interface in Java

Java SortedSet defines 6 important methods that make the set processing more convenient. These are described below:

1. Element first(): This method is used to return the first element in the invoking sorted set.

2. Element last(): This method is used to return the last element in the invoking sorted set.

3. SortedSet headSet(E toElement): This method returns the SortedSet whose element is less than toElement.

4. SortedSet tailSet(E fromElement): This method returns the SortedSet whose elements are greater than or equal to fromElement.

5. SortedSet subSet(E fromElement, E toElement): This method returns the SortedSet whose elements range from fromElement inclusive to toElement exclusive.

6. Comparator<? super E> comparator(): This method returns the sorted set comparator and it returns null if the natural ordering is used for this set.

Java SortedSet Example:

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

class sortedSetExample
{
public static void main(String args[])
{
SortedSet sset = new TreeSet();
sset.add("Javastudypoint");
sset.add("Programming");
sset.add("Coding");
sset.add("Developer");
sset.add("Tester");
sset.add("Website");

//displaying sorted set
System.out.println("Sorted Set is: " +sset);

//displaying first elemnt of sorted set
System.out.println("The first element of the sorted set is: " +sset.first());

//displaying last element of sorted set
System.out.println("The last element is given as: " +sset.last());

//returns the SortedSet whose element is less than toElement.
System.out.println("The respective element of the sorted set is: " +sset.headSet("Tester"));

//returns the SortedSet whose elements are greater than or equal to fromElement.
System.out.println("The respective element of the sorted set is: " +sset.tailSet("Programming"));

//returns the SortedSet whose elements range from fromElement inclusive to toElement exclusive.
System.out.println("The respective element of the sorted set is: " +sset.subSet("Programming","Website"));
}
}

Output:

Sorted Set is: [Coding, Developer, Javastudypoint, Programming, Tester, Website]
The first element of the sorted set is: Coding
The last element is: Website
The respective elements of the sorted set are: [Coding, Developer, Javastudypoint, Programming]
The respective elements of the sorted set are: [Programming, Tester, Website]
The respective elements of the sorted set are: [Programming, Tester]

Leave a Comment