SortedSet in Java explanation with Example

Sortedset in java with examples :

SortedSet is a interface that extends ‘Set’. You can do all set operations on its elements. Only difference is that the elements of SortedSet implemented classes are ordered. By default, all the elements are sorted in natural order.

All the elements of a SortedSet must implement ‘Comparable’ interface. That means , you can pass one comparator at the set creation time and the set can be sorted however you like.

Constructors of SortedSet implementation class :

Classes implementing SortedSet should provide four different constructors :

  1. No argument constructor : Creates an empty sorted set. Elements are sorted by natural ordering.

  2. Single Comparator argument constructor : The argument is of type ‘Comparator’.It creates an empty sorted set and elements are sorted as defined by the comparator.

  3. Single Collection argument constructor : It creates an set by taking all elements from the collection provided. Elements are sorted by natural sorting.

  4. Single SortedSet argument constructor : It creates an SortedSet by taking all elements from the argument SortedSet.

SortedSet methods :

All methods of ‘Set’ are inherited in ‘SortedSet’. So, we can use set-operations like add, addAll, clear, contains, containsAll, equals, hashCode, isEmpty, iterator, remove, removeAll, retainAll, size, toArray, toArray on ‘SortedSet’. Additionally, following methods are available in SortedSet :

  1. Comparator comparator( ) : Returns the comparator used in the sorted set. If no comparator exist, it will return null.

  2. subSet(Object fromElement, Object toElement) : It returns a subset of the SortedSet from ‘fromElement’ to ‘toElement’ including ‘fromElement’ and excluding ‘toElement’.

  3. tailSet(Object fromElement) : Returns a subset with its elements greater than or equal to ‘fromElement’.

  4. headSet(Object toElement) : Returns a subset with all elements less than ‘toElement’.

  5. Object first( ) : Returns the first or lowest element in the SortedSet

  6. Object last( ) : Returns the last or highest element in the SortedSet.

Example of a SortedSet class :

‘java.util.TreeSet’ class implements SortedSet interface. In this example, I will show you one example of this class :

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

public class Main {

    public static void main(String[] args) {
        //create one 'TreeSet' object
        SortedSet set = new TreeSet();

        //add elements to the set

        //iterate over the elements and print out the results
        Iterator iterator = set.iterator();

            System.out.print(" ");


        //print one subset

        //print one tailset

        //print one headset

        //print the first element

        //print the last element

It will print the following output :

apple ball cat dog elephant fox goat hat 
[dog, elephant, fox, goat]
[dog, elephant, fox, goat, hat]
[apple, ball, cat]

Similar tutorials :