Sunday, 27 April 2014

TreeSet : tailSet ( E fromElement, boolean inclusive ) : get the Tail Set of this set from specific element

public 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 TreeSetTailSetFlag{
 public static void main(String args[]){
  TreeSet<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 TreeSetTailSetFlag1{
 public static void main(String args[]){
  TreeSet<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 uses natural ordering, or its comparator does not permit null elements

import java.util.*;

class TreeSetTailSet1NullPointer{
 public static void main(String args[]){
  TreeSet<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 TreeSetTailSet1NullPointer.main(TreeSetTailSet1NullPointer.java:17)


Prevoius                                                 Next                                                 Home

No comments:

Post a Comment