Sunday, 20 April 2014

SortedSet : headSet : get the Head Set of this set upto specific element

SortedSet<E> headSet(E toElement)
Returns a view of the portion of this set whose elements are strictly less than toElement. 

import java.util.*;

class SortedSetHeadSet{
 public static void main(String args[]){
  SortedSet<Integer> mySet1 = new TreeSet<>();
  SortedSet<Integer> headSet;
  
  /* 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 headSet upto 10");
  headSet = mySet1.headSet(11);
  
  System.out.println("\nElements in headSet are");
  System.out.println(headSet);
 }
}

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

get the headSet upto 10

Elements in headSet are
[2, 4, 6, 8, 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 SortedSetHeadSet1{
 public static void main(String args[]){
  SortedSet<Integer> mySet1 = new TreeSet<>();
  SortedSet<Integer> headSet;
  
  /* 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 headSet upto 10");
  headSet = mySet1.headSet(11);
  
  System.out.println("\nElements in headSet are");
  System.out.println(headSet);
  
  /* Adding 1,3,5,7,9 to headSet */
  headSet.add(1);
  headSet.add(3);
  headSet.add(5);
  headSet.add(7);
  headSet.add(9);
  
  /* Adding 2,4,6,8,10 to mySet1 */
  mySet1.remove(2);
  mySet1.remove(4);
  mySet1.remove(6);
  mySet1.remove(8);
  mySet1.remove(10);
  
  System.out.println("\nElements in mySet1 are");
  System.out.println(mySet1);
  
  System.out.println("\nElements in headSet are");
  System.out.println(headSet);
  
 }
}

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

get the headSet upto 10

Elements in headSet are
[2, 4, 6, 8, 10]

Elements in mySet1 are
[1, 3, 5, 7, 9, 12, 14, 16, 18, 20]

Elements in headSet are
[1, 3, 5, 7, 9]


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

import java.util.*;

class SortedSetHeadSetIllegal{
 public static void main(String args[]){
  SortedSet<Integer> mySet1 = new TreeSet<>();
  SortedSet<Integer> subSet = new TreeSet<>();
  SortedSet<Integer> headSet;
  
  /* Add Elements to mySet1 */
  for(int i=20; i > 0; i-=2)
   mySet1.add(i);
   
  subSet = mySet1.subSet(5, 15);
   
  System.out.println("Elements in mySet1 are");
  System.out.println(mySet1);
  
  System.out.println("Elements in subSet are");
  System.out.println(subSet);
  
  System.out.println("\nget the headSet upto 17 from subSet");
  headSet = subSet.headSet(17);
  
  System.out.println("\nElements in headSet are");
  System.out.println(headSet);
 }
}

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

get the headSet upto 17 from subSet
Exception in thread "main" java.lang.IllegalArgumentException: toKey out of range
        at java.util.TreeMap$AscendingSubMap.headMap(TreeMap.java:1719)
        at java.util.TreeSet.headSet(TreeSet.java:338)
        at java.util.TreeSet.headSet(TreeSet.java:372)
        at SortedSetHeadSetIllegal.main(SortedSetHeadSetIllegal.java:22)


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

import java.util.*;

class SortedSetHeadSetNullPointer{
 public static void main(String args[]){
  SortedSet<Integer> mySet1 = new TreeSet<>();
  SortedSet<Integer> headSet;
  
  /* 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 headSet upto null");
  headSet = mySet1.headSet(null);
  
  System.out.println("\nElements in headSet are");
  System.out.println(headSet);
 }
}

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

get the headSet 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:1266)
        at java.util.TreeMap$AscendingSubMap.<init>(TreeMap.java:1699)
        at java.util.TreeMap.headMap(TreeMap.java:891)
        at java.util.TreeSet.headSet(TreeSet.java:338)
        at java.util.TreeSet.headSet(TreeSet.java:372)
        at SortedSetHeadSetNullPointer.main(SortedSetHeadSetNullPointer.java:16)


4. throws ClassCastException if toElement is not compatible with this set's comparator (or, if the set has no comparator, if toElement does not implement Comparable.

class Employee{

}

import java.util.*;

class SortedSetHeadSetClassCast{
 public static void main(String args[]){
  SortedSet<Employee> mySet1 = new TreeSet<>();
  SortedSet<Employee> subSet1;
  
  Employee e1 = new Employee();
  
  subSet1 = mySet1.headSet(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:1266)
        at java.util.TreeMap$AscendingSubMap.<init>(TreeMap.java:1699)
        at java.util.TreeMap.headMap(TreeMap.java:891)
        at java.util.TreeSet.headSet(TreeSet.java:338)
        at java.util.TreeSet.headSet(TreeSet.java:372)
        at SortedSetHeadSetClassCast.main(SortedSetHeadSetClassCast.java:10)




 
Prevoius                                                 Next                                                 Home

No comments:

Post a Comment