Monday, 21 April 2014

NavigableSet : subSet (E fromElement, boolean fromInclusive, E toElement, boolean toInclusive)

subSet(E fromElement, boolean fromInclusive, E toElement, boolean toInclusive)
Returns a view of the portion of this set whose elements range from fromElement to toElement. If fromInclusive, toInclusive are true, then it considers the fromElement and toElement.

import java.util.*;

class NavigableSetSubSet{
 public static void main(String args[]){
  NavigableSet<Integer> mySet1 = new TreeSet<> ();
  NavigableSet<Integer> subSet1;
  NavigableSet<Integer> subSet2;
  NavigableSet<Integer> subSet3;
  NavigableSet<Integer> subSet4;
  
  /* 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, false, 4, false);
  subSet2 = mySet1.subSet(2, false, 4, true);
  subSet3 = mySet1.subSet(2, true, 4, false);
  subSet4 = mySet1.subSet(2, true, 4, true);
  
  System.out.println("Elements in subSet1 are");
  System.out.println(subSet1+"\n");
  
  System.out.println("Elements in subSet2 are");
  System.out.println(subSet2+"\n");
  
  System.out.println("Elements in subSet3 are");
  System.out.println(subSet3+"\n");
  
  System.out.println("Elements in subSet4 are");
  System.out.println(subSet4+"\n");
 }
}

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

Elements in subSet1 are
[]

Elements in subSet2 are
[4]

Elements in subSet3 are
[2]

Elements in subSet4 are
[2, 4]

1. If fromElement and toElement are equal, the returned set is empty unless fromInclusive and toInclusive are both true.

import java.util.*;

class NavigableSetSubSet1{
 public static void main(String args[]){
  NavigableSet<Integer> mySet1 = new TreeSet<> ();
  NavigableSet<Integer> subSet1;
  NavigableSet<Integer> subSet2;
  NavigableSet<Integer> subSet3;
  NavigableSet<Integer> subSet4;
  
  /* 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, false, 2, false);
  subSet2 = mySet1.subSet(2, false, 2, true);
  subSet3 = mySet1.subSet(2, true, 2, false);
  subSet4 = mySet1.subSet(2, true, 2, true);
  
  System.out.println("Elements in subSet1 are");
  System.out.println(subSet1+"\n");
  
  System.out.println("Elements in subSet2 are");
  System.out.println(subSet2+"\n");
  
  System.out.println("Elements in subSet3 are");
  System.out.println(subSet3+"\n");
  
  System.out.println("Elements in subSet4 are");
  System.out.println(subSet4+"\n");
 }
}

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

Elements in subSet1 are
[]

Elements in subSet2 are
[]

Elements in subSet3 are
[]

Elements in subSet4 are
[2]

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 NavigableSetSubSet2{
 public static void main(String args[]){
  NavigableSet<Integer> mySet1 = new TreeSet<> ();
  NavigableSet<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, false, 8, false);
  
  System.out.println("Elements in subSet1 are");
  System.out.println(subSet1+"\n");
  
  System.out.println("\nAdding 3 to subSet1");
  subSet1.add(3);
  
  System.out.println("Removing 4, 6 from mySet1");
  mySet1.remove(4);
  mySet1.remove(6);
  
  System.out.println("\nElements in mySet1 are");
  System.out.println(mySet1+"\n");
  
  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
[4, 6]


Adding 3 to subSet1
Removing 4, 6 from mySet1

Elements in mySet1 are
[2, 3, 8, 10]

Elements in subSet1 are
[3]

3. Throws ClassCastException , if fromElement is not compatible with this set's comparator (or, if the set has no comparator, if fromElement does not implement Comparable).

4. throws NullPointerException if fromElement or toElement} is null and this set does not permit null elements

import java.util.*;

class NavigableSetNullPointer{
 public static void main(String args[]){
  NavigableSet<Integer> mySet1 = new TreeSet<> ();
  NavigableSet<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(null, false, 4, false);
 }
}

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

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 NavigableSetNullPointer.main(NavigableSetNullPointer.java:16)

5. 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.

import java.util.*;

class NavigableSetIllegalArg{
 public static void main(String args[]){
  NavigableSet<Integer> mySet1 = new TreeSet<> ();
  NavigableSet<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(6, false, 4, false);
 }
}

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

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 NavigableSetIllegalArg.main(NavigableSetIllegalArg.java:16)




Prevoius                                                 Next                                                 Home

No comments:

Post a Comment