Monday 21 April 2014

NavigableSet : subSet (E fromElement, E toElement)

SortedSet<E> subSet(E fromElement, E toElement)
Returns a view of the portion of this set whose elements range from fromElement, inclusive, to toElement, exclusive.

import java.util.*;

class NavigableSetSubSetEx{
 public static void main(String args[]){
  NavigableSet<Integer> mySet1 = new TreeSet<> ();
  SortedSet<Integer> subSet1;
  
  /* Add Elements too mySet1 */
  for(int i=10; i>0; i-=2){
   mySet1.add(i);
  }
  
  System.out.println("Elements in mySet1 are");
  System.out.println(mySet1+"\n");
  
  subSet1 = mySet1.subSet(2, 10);

  System.out.println("Elements in subSet1 are");
  System.out.println(subSet1+"\n");
 }
}

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

Elements in subSet1 are
[2, 4, 6, 8]

1. If fromElement and toElement are equal, the returned set is empty.

import java.util.*;

class NavigableSetSubSetEx1{
 public static void main(String args[]){
  NavigableSet<Integer> mySet1 = new TreeSet<>();
  SortedSet<Integer> subSet;
  
  /* Add Elements to mySet1 */
  for(int i=10; i > 0; i--)
   mySet1.add(i);
   
  System.out.println("Elements in mySet1 are");
  System.out.println(mySet1);
  
  System.out.println("\nget the subset from Elements 4 to 4");
  subSet = mySet1.subSet(4, 4);
  
  System.out.println("\nElements in subSet are");
  System.out.println(subSet);
 }
}

Output
Elements in mySet1 are
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

get the subset from Elements 4 to 4

Elements in subSet are
[]

2. 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 NavigableSetSubSetEx2{
 public static void main(String args[]){
  NavigableSet<Integer> mySet1 = new TreeSet<>();
  SortedSet<Integer> subSet;
  
  /* Add Elements to mySet1 */
  for(int i=10; i > 0; i--)
   mySet1.add(i);
   
  System.out.println("Elements in mySet1 are");
  System.out.println(mySet1);
  
  System.out.println("\nget the subset from Elements 4 to 8");
  subSet = mySet1.subSet(4, 9);
  
  System.out.println("\nElements in subSet are");
  System.out.println(subSet);
  
  System.out.println("\nRemoving 6 from subSet");
  subSet.remove(6);

  System.out.println("\nElements in mySet1 are");
  System.out.println(mySet1);
  System.out.println("Elements in subSet are");
  System.out.println(subSet);
 }
}

Output
Elements in mySet1 are
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

get the subset from Elements 4 to 8

Elements in subSet are
[4, 5, 6, 7, 8]

Removing 6 from subSet

Elements in mySet1 are
[1, 2, 3, 4, 5, 7, 8, 9, 10]
Elements in subSet are
[4, 5, 7, 8]

3. throws NullPointerException if fromElement or toElement is null and this set does not permit null elements.
import java.util.*;

class NavigableSetSubSetNullPointer{
 public static void main(String args[]){
  NavigableSet<Integer> mySet1 = new TreeSet<>();
  SortedSet<Integer> subSet;
  
  /* Add Elements to mySet1 */
  for(int i=10; i > 0; i--)
   mySet1.add(i);
   
  System.out.println("Elements in mySet1 are");
  System.out.println(mySet1);
  
  System.out.println("\nget the subset from Elements null to 8");
  subSet = mySet1.subSet(null, 9);
  
  System.out.println("\nElements in subSet are");
  System.out.println(subSet);
 }
}

Output
Elements in mySet1 are
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

get the subset from Elements null to 8
Exception in thread "main" java.lang.NullPointerException
        at java.util.TreeMap.compare(TreeMap.java:1188)
        at java.util.TreeMap$NavigableSubMap.<init>(TreeMap.java:1260)
        at java.util.TreeMap$AscendingSubMap.<init>(TreeMap.java:1699)
        at java.util.TreeMap.subMap(TreeMap.java:877)
        at java.util.TreeSet.subSet(TreeSet.java:325)
        at java.util.TreeSet.subSet(TreeSet.java:361)
        at NavigableSetSubSetNullPointer.main(NavigableSetSubSetNullPointer.java:16)

4. throws IllegalArgumentException if fromElement is greater than toElement or if this set itself has a restricted range, and fromElement or
toElement lies outside the bounds of the range
Example 1
import java.util.*;

class NavigableSetSubSetIllegalArgument1{
 public static void main(String args[]){
  NavigableSet<Integer> mySet1 = new TreeSet<>();
  SortedSet<Integer> subSet;
  
  /* Add Elements to mySet1 */
  for(int i=10; i > 0; i--)
   mySet1.add(i);
   
  System.out.println("Elements in mySet1 are");
  System.out.println(mySet1);
  
  System.out.println("\nget the subset from Elements 8 to 4");
  subSet = mySet1.subSet(8, 5);
  
  System.out.println("\nElements in subSet are");
  System.out.println(subSet);
 }
}

Output
Elements in mySet1 are
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

get the subset from Elements 8 to 4
Exception in thread "main" java.lang.IllegalArgumentException: fromKey > toKey
        at java.util.TreeMap$NavigableSubMap.<init>(TreeMap.java:1261)
        at java.util.TreeMap$AscendingSubMap.<init>(TreeMap.java:1699)
        at java.util.TreeMap.subMap(TreeMap.java:877)
        at java.util.TreeSet.subSet(TreeSet.java:325)
        at java.util.TreeSet.subSet(TreeSet.java:361)
        at NavigableSetSubSetIllegalArgument1.main(NavigableSetSubSetIllegalArgument1.java:16)

Example 2
import java.util.*;

class NavigableSetSubSetIllegalArgument2{
 public static void main(String args[]){
  NavigableSet<Integer> mySet1 = new TreeSet<>();
  SortedSet<Integer> subSet1;
  SortedSet<Integer> subSet2;
  
  /* Add Elements to mySet1 */
  for(int i=10; i > 0; i--)
   mySet1.add(i);
   
  System.out.println("Elements in mySet1 are");
  System.out.println(mySet1);
  
  System.out.println("\nget the subset from Elements 4 to 8");
  subSet1 = mySet1.subSet(4, 9);
  
  System.out.println("\nElements in subSet1 are");
  System.out.println(subSet1);
  
  System.out.println("\nget the subset from Elements 1 to 4 from subSet1");
  subSet2 = subSet1.subSet(1, 5); 
 }
}

Output
Elements in mySet1 are
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

get the subset from Elements 4 to 8

Elements in subSet1 are
[4, 5, 6, 7, 8]

get the subset from Elements 1 to 4 from subSet1
Exception in thread "main" java.lang.IllegalArgumentException: fromKey out of ra
nge
        at java.util.TreeMap$AscendingSubMap.subMap(TreeMap.java:1709)
        at java.util.TreeSet.subSet(TreeSet.java:325)
        at java.util.TreeSet.subSet(TreeSet.java:361)
        at NavigableSetSubSetIllegalArgument2.main(NavigableSetSubSetIllegalArgument2.java:23)


5. throws ClassCastException if fromElement and toElement cannot be compared to one another using this set's comparator (or, if the set has no comparator, using natural ordering).

class Employee{

}

import java.util.*;

class NavigableSetSubSetClassCast{
 public static void main(String args[]){
  NavigableSet<Employee> mySet1 = new TreeSet<>();
  SortedSet<Employee> subSet1;
  
  Employee e1 = new Employee();
  Employee e2 = new Employee();
  
  subSet1 = mySet1.subSet(e1, e2); 
 }
}

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:1260)
        at java.util.TreeMap$AscendingSubMap.<init>(TreeMap.java:1699)
        at java.util.TreeMap.subMap(TreeMap.java:877)
        at java.util.TreeSet.subSet(TreeSet.java:325)
        at java.util.TreeSet.subSet(TreeSet.java:361)
        at NavigableSetSubSetClassCast.main(NavigableSetSubSetClassCast.java:11)

Since Employee class not implemented the Comparable interface, so ClassCastException thrown.



Prevoius                                                 Next                                                 Home

No comments:

Post a Comment