SortedSet interface in Java with Example

SoretedSet in Java is an interface which 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



Methods in Java SortedSet interface:

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<E> headSet(E toElement): This method returns the SortedSet whose elemnt are less then toElement.

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

5. SortedSet<E> 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<String> sset = new TreeSet<String>();
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]



You may enjoy these related posts:

HashSet class in Java with Example
SortedSet interface in Java with Example SortedSet interface in Java with Example Reviewed by Prashant Srivastava on March 04, 2019 Rating: 5

No comments:

Powered by Blogger.