Saturday, 1 June 2013

NavigableSet

NavigableSet method to sort the elements in ascending order, descending order, also to retrieve the element which is immediately greater than or equal to element.


import java.util.NavigableSet;
import java.util.TreeSet;


public class TestNavigableSet {

 public static void main(String[] args) {
  
   NavigableSet navigableSet=new TreeSet(); 
   
   navigableSet.add(1);
   navigableSet.add(7);
   navigableSet.add(20);
   navigableSet.add(30);
   navigableSet.add(5);
   navigableSet.add(4);
   navigableSet.add(8);

   System.out.println("NavigableSet \t"+navigableSet);
   
   navigableSet.pollFirst(); //Remove First element of the NavigableSet . 
   System.out.println("After pollFirst \t"+navigableSet);
   
   navigableSet.pollLast(); //Remove Last element of the NavigableSet.
   System.out.println("After pollLast \t"+navigableSet);
   
   navigableSet.descendingSet(); //NavigableSet set getting data in both orders: ascending and descending orders. 
   System.out.println("Descending order \t"+navigableSet.descendingSet());
  
   System.out.println("headSet \t"+navigableSet.headSet(8));// getting the all elements that is less than or equal to the given element 
   
   System.out.println("tailSet \t"+navigableSet.tailSet(20)); // getting the all elements that is greater than or equal to the given element 

   //try another method floor() ,ceiling() method etc.      
 }
}


after executing code following output shown :-