Monday 21 April 2014

NavigableSet : tailSet (E fromElement, boolean inclusive)

NavigableSet<E> tailSet (E fromElement, boolean inclusive)
Returns a view of the portion of this set whose elements are greater than (or equal to, if inclusive is true) fromElement.

import java.util.*;

class NavigableSetTailSetFlag{
 public static void main(String args[]){
  NavigableSet<Integer> mySet = new TreeSet<> ();
  
  /* Add Elements too mySet */
  for(int i=10; i>0; i-=2){
   mySet.add(i);
  }
  
  System.out.println("Elements in mySet are");
  System.out.println(mySet);
  
  for(int i=0; i<12; i+=2){
   System.out.println("\ntailSet for value " + i +" is ");
   System.out.print("inclusive is false ");
   System.out.println(mySet.tailSet(i, false));
   System.out.print("inclusive is true ");
   System.out.println(mySet.tailSet(i, true));
  }
 }
}

Output
Elements in mySet are
[2, 4, 6, 8, 10]

tailSet for value 0 is
inclusive is false [2, 4, 6, 8, 10]
inclusive is true [2, 4, 6, 8, 10]

tailSet for value 2 is
inclusive is false [4, 6, 8, 10]
inclusive is true [2, 4, 6, 8, 10]

tailSet for value 4 is
inclusive is false [6, 8, 10]
inclusive is true [4, 6, 8, 10]

tailSet for value 6 is
inclusive is false [8, 10]
inclusive is true [6, 8, 10]

tailSet for value 8 is
inclusive is false [10]
inclusive is true [8, 10]

tailSet for value 10 is
inclusive is false []
inclusive is true [10]

1. The returned set is backed by this set, so changes in the returned set are reflected in this set, and vice-versa.
import java.util.*;

class NavigableSetTailSetFlag1{
 public static void main(String args[]){
  NavigableSet<Integer> mySet = new TreeSet<> ();
  NavigableSet<Integer> tailSet;
  
  /* Add Elements too mySet */
  for(int i=10; i>0; i-=2){
   mySet.add(i);
  }
  
  tailSet = mySet.tailSet(2, true);
  
  System.out.println("Elements in mySet are");
  System.out.println(mySet+"\n");
  
  System.out.println("Elements in tailSet are");
  System.out.println(tailSet+"\n");
  
  System.out.println("\nAdding 3 to tailSet");
  tailSet.add(3);
  
  System.out.println("Remving 4 from mySet\n");
  mySet.remove(4);
  
  System.out.println("Elements in mySet are");
  System.out.println(mySet+"\n");
  
  System.out.println("Elements in tailSet are");
  System.out.println(tailSet+"\n");

 }
}

Output
Elements in mySet are
[2, 4, 6, 8, 10]

Elements in tailSet are
[2, 4, 6, 8, 10]


Adding 3 to tailSet
Remving 4 from mySet

Elements in mySet are
[2, 3, 6, 8, 10]

Elements in tailSet are
[2, 3, 6, 8, 10]

2. throws NullPointerException if fromElement is null and this set does not permit null elements

import java.util.*;

class NavigableSetTailSet1NullPointer{
 public static void main(String args[]){
  NavigableSet<Integer> mySet = new TreeSet<> ();
  NavigableSet<Integer> tailSet;
  
  /* Add Elements too mySet */
  for(int i=10; i>0; i-=2){
   mySet.add(i);
  }
  
  System.out.println("Elements in mySet are");
  System.out.println(mySet+"\n");
  
  System.out.println("Getting the Tail Set upto null");
  tailSet = mySet.tailSet(null, true);
 }
}

Output
Elements in mySet are
[2, 4, 6, 8, 10]

Getting the Tail Set upto null
Exception in thread "main" java.lang.NullPointerException
        at java.util.TreeMap.compare(TreeMap.java:1188)
        at java.util.TreeMap$NavigableSubMap.<init>(TreeMap.java:1264)
        at java.util.TreeMap$AscendingSubMap.<init>(TreeMap.java:1699)
        at java.util.TreeMap.tailMap(TreeMap.java:905)
        at java.util.TreeSet.tailSet(TreeSet.java:350)
        at NavigableSetTailSet1NullPointer.main(NavigableSetTailSet1NullPointer.
Java:17)

3. throws IllegalArgumentException if this set itself has a restricted range, and code fromElement lies outside the bounds of the range

import java.util.*;

class NavigableSetTailSetFlagIllegal{
 public static void main(String args[]){
  NavigableSet<Integer> mySet = new TreeSet<> ();
  NavigableSet<Integer> tailSet;
  
  /* Add Elements too mySet */
  for(int i=10; i>0; i-=2){
   mySet.add(i);
  }
  
  tailSet = mySet.tailSet(10, true);
  
  System.out.println("Elements in mySet are");
  System.out.println(mySet+"\n");
  
  System.out.println("Elements in tailSet are");
  System.out.println(tailSet+"\n");
  
  System.out.println("\nAdding 3 to tailSet");
  tailSet.add(3);
 }
}

Output
Elements in mySet are
[2, 4, 6, 8, 10]

Elements in tailSet are
[10]


Adding 3 to tailSet
Exception in thread "main" java.lang.IllegalArgumentException: key out of range
        at java.util.TreeMap$NavigableSubMap.put(TreeMap.java:1407)
        at java.util.TreeSet.add(TreeSet.java:255)
        at NavigableSetTailSetFlagIllegal.main(NavigableSetTailSetFlagIllegal.ja
va:22)

4. Throws ClassCastException, if fromElement is not compatible with this set's comparator (or, if the set has no comparator, if fromElement does not implement Comparable)




Prevoius                                                 Next                                                 Home

No comments:

Post a Comment