Sunday 27 April 2014

TreeSet : tailSet : get the Tail Set of this set from specific element

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 TreeSetTailSet{
 public static void main(String args[]){
  TreeSet<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);
 }
}

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]

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 TreeSetTailSet1{
 public static void main(String args[]){
  TreeSet<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);
  
  /* Adding 11,13,15,17,19 to tailSet */
  System.out.println("\nAdding 11,13,15,17,19 to tailSet");
  tailSet.add(11);
  tailSet.add(13);
  tailSet.add(15);
  tailSet.add(17);
  tailSet.add(19);
  
  /* Adding 12,14,16,18,20 to mySet1 */
  System.out.println("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
Adding 12,14,16,18,20 to 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 NullPointerException if fromElement is null and this set uses natural ordering, or its comparator does not permit null elements

import java.util.*;

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

Prevoius                                                 Next                                                 Home

No comments:

Post a Comment