Sunday, February 21, 2016

Java Sortedset Interface

| Sunday, February 21, 2016
  • SortedSet interface extends Set interface inwards collection framework of java.
  • As parent suggest, It provides full ordering on elements of Set.
  • Set elements volition live 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 live comparable. So e1.compareTo(e2) must non throw ClassCastException.
  • In collection framework, TreeSet cast is an implementation for the SortedSet interface.
SortedSet interface extends Set interface inwards collection framework of coffee Java SortedSet Interface

Few of import as well as useful methods of SortedSet Interface are every bit bellow.
  • Comparator comparator( ) : It volition render the comparator which is used to guild the elements inwards invoking set. If elements of laid upwards are ordered using natural ordering as well as then it volition render null.
  • Object first( ) : It volition render the starting fourth dimension chemical component subdivision from the set.
  • SortedSet headSet(E toElement) : It volition render those elements from SortedSet which are  strictly less than toElement. toElement volition live non included.
  • Object last() : It volition render electrical current concluding chemical component subdivision from the set.
  • SortedSet subSet(E fromElement, eastward toElement) : It volition render laid upwards of elements from SortedSet starting from fromElement to toElement. fromElement volition live included simply toElement volition live excluded.
  • SortedSet tailSet(E fromElement) : It volition render laid upwards of elements starting from fromElement.
Usage of inwards a higher house SortedSet methods is described inwards below given example.

SortedSet Example :
package JAVAExamples;  import java.util.Iterator; import java.util.SortedSet; import java.util.TreeSet;  world cast SortedsetInterface {   world static void main(String[] args) {   // Create the sorted set.   SortedSet<String> s = novel 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 laid upwards as well as print.   Iterator<String> it = s.iterator();   System.out.println("SortedSet Items are : ");   spell (it.hasNext()) {    Object chemical component subdivision = it.next();    System.out.println(element.toString());   }      //Get starting fourth dimension detail from set.   System.out.println("First detail inwards laid upwards : "+s.first());   //Get concluding detail from set.   System.out.println("Last detail inwards laid upwards : "+s.last());    // Using subSet method to become items from given FROM as well as TO elements of set.   // TO chemical component subdivision volition live excluded.   System.out.println("subSet items betwixt Delhi as well as New York : " + s.subSet("Delhi", "New York"));   // Using headSet method to become heading items from given item.   System.out.println("headSet from London : " + s.headSet("London"));   // Using tailSet method to become tailing items from given item.   System.out.println("tailSet from London : " + s.tailSet("London"));   // Check which comparator is used to variety elements.   // If render nada as well as then it has used natural ordering.   System.out.println(s.comparator());  } }

Output :
SortedSet Items are :  Chennai Delhi London Bombay New York Tokyo First detail inwards laid upwards : Chennai Last detail inwards laid upwards : Tokyo subSet items betwixt Delhi as well as New York : [Delhi, London, Mumbai] headSet from London : [Chennai, Delhi] tailSet from London : [London, Mumbai, New York, Tokyo] null

Related Posts