Java SortedSet Interface

  • SortedSet interface extends Set interface in collection framework of java.
  • As name suggest, It provides total ordering on elements of Set.
  • Set elements will be ordered using their natural ordering or using a comparator interface.
  • All the elements entries of SortedSet must implement Comparable interface.
  • All the elements of SortedSet must be comparable. So e1.compareTo(e2) must not throw ClassCastException.
  • In collection framework, TreeSet class is an implementation for the SortedSet interface.
Java SortedSet Interface hierarchy

Few important and useful methods of SortedSet Interface are as bellow.
  • Comparator comparator( ) : It will return the comparator which is used to order the elements in invoking set. If elements of set are ordered using natural ordering then it will return null.
  • Object first( ) : It will return the first element from the set.
  • SortedSet headSet(E toElement) : It will return those elements from SortedSet which are  strictly less than toElement. toElement will be not included.
  • Object last() : It will return current last element from the set.
  • SortedSet subSet(E fromElement, E toElement) : It will return set of elements from SortedSet starting from fromElement to toElement. fromElement will be included but toElement will be excluded.
  • SortedSet tailSet(E fromElement) : It will return set of elements starting from fromElement.
Usage of above SortedSet methods is described in below given example.

SortedSet Example :
package JAVAExamples;

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

public class SortedsetInterface {

 public static void main(String[] args) {
  // Create the sorted set.
  SortedSet<String> s = new TreeSet<String>();

  // Add items to the sorted.
  s.add("New York");
  s.add("Delhi");
  s.add("Tokyo");
  s.add("London");
  s.add("Mumbai");
  s.add("Chennai");

  // Iterate over the elements of set and print.
  Iterator<String> it = s.iterator();
  System.out.println("SortedSet Items are : ");
  while (it.hasNext()) {
   Object element = it.next();
   System.out.println(element.toString());
  }
  
  //Get first item from set.
  System.out.println("First item in set : "+s.first());
  //Get last item from set.
  System.out.println("Last item in set : "+s.last());

  // Using subSet method to get items from given FROM and TO elements of set.
  // TO element will be excluded.
  System.out.println("subSet items between Delhi and New York : " + s.subSet("Delhi", "New York"));
  // Using headSet method to get heading items from given item.
  System.out.println("headSet from London : " + s.headSet("London"));
  // Using tailSet method to get tailing items from given item.
  System.out.println("tailSet from London : " + s.tailSet("London"));
  // Check which comparator is used to sort elements.
  // If return null then it has used natural ordering.
  System.out.println(s.comparator());
 }
}

Output :
SortedSet Items are : 
Chennai
Delhi
London
Mumbai
New York
Tokyo
First item in set : Chennai
Last item in set : Tokyo
subSet items between Delhi and New York : [Delhi, London, Mumbai]
headSet from London : [Chennai, Delhi]
tailSet from London : [London, Mumbai, New York, Tokyo]
null

No comments:

Post a Comment