Monday 21 April 2014

NavigableSet : tailSet

SortedSet<E> tailSet(E fromElement)
Returns a view of the portion of this set whose elements are greater than or equal to fromElement.

import java.util.*;

class NavigableSetTailSet{
 public static void main(String args[]){
  NavigableSet<Integer> mySet = new TreeSet<> ();
  
  /* Add Elements too mySet */
  for(int i=20; i>0; i-=2){
   mySet.add(i);
  }
  

  System.out.println("Elements in mySet are");
  System.out.println(mySet+"\n");
  
  for(int i=1; i<22; i+=2){
   System.out.print("tailSet for value " + i +" is ");
   System.out.println(mySet.tailSet(i));
  }
 }
}

Output
Elements in mySet are
[2, 4, 6, 8, 10, 12, 14, 16, 18, 20]

tailSet for value 1 is [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
tailSet for value 3 is [4, 6, 8, 10, 12, 14, 16, 18, 20]
tailSet for value 5 is [6, 8, 10, 12, 14, 16, 18, 20]
tailSet for value 7 is [8, 10, 12, 14, 16, 18, 20]
tailSet for value 9 is [10, 12, 14, 16, 18, 20]
tailSet for value 11 is [12, 14, 16, 18, 20]
tailSet for value 13 is [14, 16, 18, 20]
tailSet for value 15 is [16, 18, 20]
tailSet for value 17 is [18, 20]
tailSet for value 19 is [20]
tailSet for value 21 is []

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 NavigableSetTailSet1{
 public static void main(String args[]){
  NavigableSet<Integer> mySet1 = new TreeSet<>();
  SortedSet<Integer> tailSet;
  
  /* Add Elements to mySet1 */
  for(int i=20; i > 0; i-=2)
   mySet1.add(i);
   
  System.out.println("Elements in mySet1 are");
  System.out.println(mySet1);
  
  System.out.println("\nget the tailSet from 10");
  tailSet = mySet1.tailSet(10);
  
  System.out.println("\nElements in tailSet are");
  System.out.println(tailSet);
  
  System.out.println("\nAdding 11,13,15,17,19 to tailSet");
  /* Adding 11,13,15,17,19 to tailSet */
  tailSet.add(11);
  tailSet.add(13);
  tailSet.add(15);
  tailSet.add(17);
  tailSet.add(19);
  
  System.out.println("Removing 12,14,16,18,20 from mySet1");
  /* Adding 12,14,16,18,20 to mySet1 */
  mySet1.remove(12);
  mySet1.remove(14);
  mySet1.remove(16);
  mySet1.remove(18);
  mySet1.remove(20);
  
  System.out.println("\nElements in mySet1 are");
  System.out.println(mySet1);
  
  System.out.println("\nElements in tailSet are");
  System.out.println(tailSet);
  
 }
}

Output
Elements in mySet1 are
[2, 4, 6, 8, 10, 12, 14, 16, 18, 20]

get the tailSet from 10

Elements in tailSet are
[10, 12, 14, 16, 18, 20]

Adding 11,13,15,17,19 to tailSet
Removing 12,14,16,18,20 from mySet1

Elements in mySet1 are
[2, 4, 6, 8, 10, 11, 13, 15, 17, 19]

Elements in tailSet are
[10, 11, 13, 15, 17, 19]

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

import java.util.*;

class NavigableSetTailSetIllegal{
 public static void main(String args[]){
  NavigableSet<Integer> mySet1 = new TreeSet<>();
  SortedSet<Integer> subSet;
  SortedSet<Integer> tailSet;
  
  /* Add Elements to mySet1 */
  for(int i=20; i > 0; i-=2)
   mySet1.add(i);
   
  subSet = mySet1.subSet(10, 15);
  
  System.out.println("Elements in subSet are");
  System.out.println(subSet);
  
  System.out.println("Get the tailSet of subSet from 16");
  tailSet = subSet.tailSet(16);
  
 }
}

Output
Elements in subSet are
[10, 12, 14]
Get the tailSet of subSet from 16
Exception in thread "main" java.lang.IllegalArgumentException: fromKey out of ra
nge
        at java.util.TreeMap$AscendingSubMap.tailMap(TreeMap.java:1727)
        at java.util.TreeSet.tailSet(TreeSet.java:350)
        at java.util.TreeSet.tailSet(TreeSet.java:383)
        at NavigableSetTailSetIllegal.main(NavigableSetTailSetIllegal.java:19)

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

import java.util.*;

class NavigableSetTailSetNullPointer{
 public static void main(String args[]){
  NavigableSet<Integer> mySet1 = new TreeSet<>();
  SortedSet<Integer> tailSet;
  
  /* Add Elements to mySet1 */
  for(int i=20; i > 0; i-=2)
   mySet1.add(i);
   
  tailSet = mySet1.tailSet(null); 
 }
}

Output
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 java.util.TreeSet.tailSet(TreeSet.java:383)
        at NavigableSetTailSetNullPointer.main(NavigableSetTailSetNullPointer.ja
va:12)

4. throws ClassCastException if fromElement is not compatible with this set's comparator

class Employee{

}

import java.util.*;

class NavigableSetTailSetClassCast{
 public static void main(String args[]){
  NavigableSet<Employee> mySet1 = new TreeSet<>();
  SortedSet<Employee> tailSet;
  
  Employee e1 = new Employee();
   
  tailSet = mySet1.tailSet(e1); 
 }
}

Output
Exception in thread "main" java.lang.ClassCastException: Employee cannot be cast
 to java.lang.Comparable
        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 java.util.TreeSet.tailSet(TreeSet.java:383)
        at NavigableSetTailSetClassCast.main(NavigableSetTailSetClassCast.java:10)




Prevoius                                                 Next                                                 Home

No comments:

Post a Comment