Curriculum
In Java, SortedSet
is an interface that extends the Set
interface and is used to store a collection of unique elements in a sorted manner. This means that the elements are sorted in either ascending or descending order based on their natural ordering or a custom ordering provided by the user. The SortedSet
interface provides several methods to perform operations on the sorted set of elements.
Here is an example of how to use SortedSet
:
import java.util.*; public class SortedSetExample { public static void main(String[] args) { SortedSet<String> fruits = new TreeSet<>(); fruits.add("Apple"); fruits.add("Banana"); fruits.add("Grapes"); fruits.add("Orange"); System.out.println("Sorted set of fruits: " + fruits); SortedSet<Integer> numbers = new TreeSet<>(); numbers.add(10); numbers.add(50); numbers.add(20); numbers.add(40); System.out.println("Sorted set of numbers: " + numbers); } }
In this example, we first create a SortedSet
of String
objects called fruits
and add some elements to it using the add()
method. We then print the sorted set of fruits using the toString()
method.
We also create a SortedSet
of Integer
objects called numbers
and add some elements to it using the add()
method. We then print the sorted set of numbers using the toString()
method.
Here are some important methods provided by the SortedSet
interface:
comparator()
– Returns the Comparator
used to sort the elements of the set, or null
if the natural ordering is used.first()
– Returns the first (lowest) element in the set.last()
– Returns the last (highest) element in the set.headSet(E toElement)
– Returns a sorted set containing elements that are less than the specified element.tailSet(E fromElement)
– Returns a sorted set containing elements that are greater than or equal to the specified element.subSet(E fromElement, E toElement)
– Returns a sorted set containing elements that are greater than or equal to the first specified element and less than the second specified element.These methods can be used to perform various operations on the SortedSet
.
In addition to the above methods, SortedSet
also provides all the methods of the Set
interface, such as add()
, remove()
, contains()
, isEmpty()
, size()
, and clear()
.
It is important to note that the SortedSet
interface does not allow duplicate elements. If you try to add a duplicate element to a SortedSet
, it will simply be ignored. Also, the elements of a SortedSet
must implement the Comparable
interface or you need to provide a custom Comparator
to the TreeSet
constructor to specify the order in which the elements are sorted.
Here is an example of using a custom Comparator
to sort a SortedSet
:
import java.util.*; public class SortedSetExample { public static void main(String[] args) { SortedSet<String> fruits = new TreeSet<>(new LengthComparator()); fruits.add("Apple"); fruits.add("Banana"); fruits.add("Grapes"); fruits.add("Orange"); System.out.println("Sorted set of fruits: " + fruits); } } class LengthComparator implements Comparator<String> { public int compare(String s1, String s2) { return s1.length() - s2.length(); } }
In this example, we create a SortedSet
of String
objects called fruits
and pass a custom Comparator
called LengthComparator
to the TreeSet
constructor. The LengthComparator
class implements the Comparator
interface and sorts the elements based on their length. We then add some elements to the SortedSet
and print the sorted set of fruits.
Overall, the SortedSet
interface provides a way to store a collection of unique elements in a sorted manner, making it easy to retrieve the elements in a sorted order.