Java SortedSet Interface

  • SortedSet interface extends Set interface inwards collection framework of java.
  • As cite 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 shape is an implementation for the SortedSet interface.
Java SortedSet Interface hierarchy

Few of import as well as useful methods of SortedSet Interface are equally bellow.
  • Comparator comparator( ) : It volition render the comparator which is used to lodge the elements inwards invoking set. If elements of fix are ordered using natural ordering as well as then it volition render null.
  • Object first( ) : It volition render the get-go chemical ingredient 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 flow final chemical ingredient from the set.
  • SortedSet subSet(E fromElement, due east toElement) : It volition render fix of elements from SortedSet starting from fromElement to toElement. fromElement volition live included only toElement volition live excluded.
  • SortedSet tailSet(E fromElement) : It volition render fix of elements starting from fromElement.
Usage of higher upwardly 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 shape SortedsetInterface {   world static void main(String[] args) {   // Create the sorted set.   SortedSet<String> second = 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 fix as well as print.   Iterator<String> it = s.iterator();   System.out.println("SortedSet Items are : ");   piece (it.hasNext()) {    Object chemical ingredient = it.next();    System.out.println(element.toString());   }      //Get get-go especial from set.   System.out.println("First especial inwards fix : "+s.first());   //Get final especial from set.   System.out.println("Last especial inwards fix : "+s.last());    // Using subSet method to become items from given FROM as well as TO elements of set.   // TO chemical ingredient 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 kind elements.   // If render naught 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 especial inwards fix : Chennai Last especial inwards fix : 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


More interesting articles here :Generation Enggelmundus Internet Marketing Tool here :Zeageat IM http://www.software-testing-tutorials-automation.com/
Post a Comment (0)
Previous Post Next Post