In Java, the SortedSet
interface is a subtype of the Set
interface that represents a set of elements sorted in a specific order. It extends the Set
interface and adds methods for handling sorted sets. SortedSet
is implemented by classes like TreeSet
and is part of the Java Collections Framework. Here are some key characteristics and methods of the SortedSet
interface:
-
Ordering: A
SortedSet
maintains its elements in a specific order defined by a comparator or the natural ordering of the elements. This ordering allows for efficient retrieval of elements in sorted order. -
Duplicates: Like the
Set
interface, aSortedSet
does not allow duplicate elements. If you try to add a duplicate element, it won't be stored. -
Comparator: You can specify a custom comparator to define the sorting order of elements in the
SortedSet
. If you don't provide a comparator, the natural ordering of elements (if they implement theComparable
interface) will be used. -
Methods: Some of the important methods provided by the
SortedSet
interface include:comparator()
: Returns the comparator used to order elements in the set or null if natural ordering is used.first()
: Returns the first (lowest) element in the set.last()
: Returns the last (highest) element in the set.headSet(toElement)
: Returns a view of the portion of the set that is strictly less thantoElement
.tailSet(fromElement)
: Returns a view of the portion of the set that is greater than or equal tofromElement
.subSet(fromElement, toElement)
: Returns a view of the portion of the set that is greater than or equal tofromElement
and strictly less thantoElement
.
Here's an example of how to use the SortedSet
interface with a TreeSet
:
import java.util.SortedSet;
import java.util.TreeSet;
public class SortedSetExample {
public static void main(String[] args) {
// Creating a SortedSet (TreeSet) of integers
SortedSet<Integer> sortedSet = new TreeSet<>();
// Adding elements
sortedSet.add(5);
sortedSet.add(2);
sortedSet.add(8);
sortedSet.add(1);
// Printing the sorted set
System.out.println(sortedSet); // Output: [1, 2, 5, 8]
// Using methods of the SortedSet interface
System.out.println("First element: " + sortedSet.first()); // Output: 1
System.out.println("Last element: " + sortedSet.last()); // Output: 8
// Subsetting the set
SortedSet<Integer> subset = sortedSet.subSet(2, 5); // [2, 5]
System.out.println("Subset: " + subset);
}
}
In this example, we create a TreeSet
, which implements the SortedSet
interface, and add integers to it. The elements are automatically sorted in ascending order, and we can use methods like first()
and last()
to retrieve the first and last elements or subSet()
to get a subset of elements within a specified range.