Monday 21 April 2014

NavigableSet : descendingSet: Get the Descending Set

NavigableSet<E> descendingSet()
Returns a reverse order view of the elements contained in this set.

import java.util.*;

class NavigableSetDescendingSet{
 public static void main(String args[]){
  NavigableSet<Integer> mySet = new TreeSet<> ();
  NavigableSet<Integer> descendSet;
  
  for(int i=0; i < 15; i++)
   mySet.add(i);
   
  System.out.println("Elements in mySet are");
  System.out.println(mySet+"\n");
  
  descendSet = mySet.descendingSet();
  
  System.out.println("Elements in descendSet are");
  System.out.println(descendSet);  
 }
}

Output
Elements in mySet are
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]

Elements in descendSet are
[14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

1. The descending set is backed by this set, so changes to the set are
reflected in the descending set, and vice-versa.

import java.util.*;

class NavigableSetDescendingSet1{
 public static void main(String args[]){
  NavigableSet<Integer> mySet = new TreeSet<> ();
  NavigableSet<Integer> descendSet;
  
  for(int i=0; i < 15; i++)
   mySet.add(i);
   
  System.out.println("Elements in mySet are");
  System.out.println(mySet+"\n");
  
  descendSet = mySet.descendingSet();
  
  System.out.println("Elements in descendSet are");
  System.out.println(descendSet);  
  
  System.out.println("\nAdding 99 to descendSet");
  descendSet.add(99);
  
  System.out.println("Removing 0 from mySet");
  mySet.remove(0);
  
  System.out.println("\nElements in mySet are");
  System.out.println(mySet+"\n");
  
  System.out.println("Elements in descendSet are");
  System.out.println(descendSet);   
 }
}

Output
Elements in mySet are
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]

Elements in descendSet are
[14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

Adding 99 to descendSet
Removing 0 from mySet

Elements in mySet are
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 99]

Elements in descendSet are
[99, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]




Prevoius                                                 Next                                                 Home

No comments:

Post a Comment