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 :
-
No argument constructor : Creates an empty sorted set. Elements are sorted by natural ordering.
-
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.
-
Single Collection argument constructor : It creates an set by taking all elements from the collection provided. Elements are sorted by natural sorting.
-
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 :
-
Comparator comparator( ) : Returns the comparator used in the sorted set. If no comparator exist, it will return null.
-
subSet(Object fromElement, Object toElement) : It returns a subset of the SortedSet from ‘fromElement’ to ‘toElement’ including ‘fromElement’ and excluding ‘toElement’.
-
tailSet(Object fromElement) : Returns a subset with its elements greater than or equal to ‘fromElement’.
-
headSet(Object toElement) : Returns a subset with all elements less than ‘toElement’.
-
Object first( ) : Returns the first or lowest element in the SortedSet
-
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
set.add("apple");
set.add("fox");
set.add("goat");
set.add("dog");
set.add("elephant");
set.add("ball");
set.add("hat");
set.add("cat");
//iterate over the elements and print out the results
Iterator iterator = set.iterator();
while(iterator.hasNext()){
System.out.print(iterator.next().toString()+" ");
}
System.out.println();
//print one subset
System.out.println(set.subSet("dog","hat"));
//print one tailset
System.out.println(set.tailSet("dog"));
//print one headset
System.out.println(set.headSet("dog"));
//print the first element
System.out.println(set.first());
//print the last element
System.out.println(set.last());
}
}
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]
apple
hat