Sunday 20 April 2014

SortedSet : subSet : Get the subset

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

import java.util.*;

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

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]

1. If fromElement and toElement are equal, the returned set is empty.
import java.util.*;

class SortedSetSubSet1{
 public static void main(String args[]){
  SortedSet<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 SortedSetSubSet2{
 public static void main(String args[]){
  SortedSet<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</tt> is null and this set does not permit null elements.

import java.util.*;

class SortedSetSubSetNullPointer{
 public static void main(String args[]){
  SortedSet<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 SortedSetSubSetNullPointer.main(SortedSetSubSetNullPointer.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 SortedSetSubSetIllegalArgument1{
 public static void main(String args[]){
  SortedSet<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 SortedSetSubSetIllegalArgument1.main(SortedSetSubSetIllegalArgument1.
Java:16)

Example 2
import java.util.*;

class SortedSetSubSetIllegalArgument2{
 public static void main(String args[]){
  SortedSet<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 SortedSetSubSetIllegalArgument2.main(SortedSetSubSetIllegalArgument2.
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 SortedSetSubSetClassCast{
 public static void main(String args[]){
  SortedSet<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 SortedSetSubSetClassCast.main(SortedSetSubSetClassCast.java:11)

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




Prevoius                                                 Next                                                 Home

No comments:

Post a Comment