java.util.TreeSet.subSet(E fromElement, boolean fromInclusive, E toElement, boolean toInclusive) - Walking Techie

Blog about Java programming, Design Pattern, and Data Structure.

Saturday, December 17, 2016

java.util.TreeSet.subSet(E fromElement, boolean fromInclusive, E toElement, boolean toInclusive)

What is Java TreeSet

TreeSet creates a collection that uses a tree for storage. Objects are stored in sorted, ascending order according to the natural order. Optionally, you can construct a TreeSet with a constructor that lets you give the collection your own rules for what the order should be (rather than relying on the ordering defined by the elements' class) by using a Comparable or Comparator.

Description of subSet(E fromElement, boolean fromInclusive, E toElement, boolean toInclusive)

The subSet(E fromElement, boolean fromInclusive, E toElement, boolean toInclusive) method returns a view of the portion of this set whose elements range from fromElement to toElement. If fromElement and toElement are equal, the returned set is empty unless fromInclusive and toInclusive are both true. The returned set is backed by this set, so changes in the returned set are reflected in this set, and vice-versa. The returned set supports all optional set operations that this set supports. The returned set will throw an IllegalArgumentException on an attempt to insert an element outside its range.

Declaration

Following is the declaration for java.util.TreeSet.subSet(E fromElement, boolean fromInclusive, E toElement, boolean toInclusive) method.

public NavigableSet<E> subSet(E fromElement, boolean fromInclusive, E toElement, boolean toInclusive)
Parameters :
  • fromElement -> Low endpoint of the returned set.
  • fromInclusive -> This is true if the low endpoint is to be included in the returned view.
  • toElement -> High endpoint of the returned set.
  • toInclusive -> This is true if the high endpoint is to be included in the returned view.

Return value : Returns a view of the portion of this set whose elements range from fromElement to toElement.

Exception :
  • 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).
  • NullPointerException -> If fromElement or toElement is null and this set uses natural ordering, or its comparator does not permit null elements.
  • 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.

Simple example of TreeSet.subSet(E fromElement, boolean fromInclusive, E toElement, boolean toInclusive)

The following example shows the usage of java.util.TreeSet.suSet() method.

package com.walking.techie;

import java.util.TreeSet;

public class TreeSetSubSet {

 public static void main(String[] args) {
  TreeSet<Integer> treeSet = new TreeSet<Integer>();
  treeSet.add(23);
  treeSet.add(34);
  treeSet.add(13);
  treeSet.add(2);
  treeSet.add(28);

  // printing the elements of tree set using for-each
  System.out.println("***Elements of the treeSet***");
  for (Integer val : treeSet) {
   System.out.println(val);
  }

  // creating subset from tree set, fromElement exclusive and toElement
  // inclusive
  TreeSet<Integer> treeSubSet = (TreeSet<Integer>) treeSet.subSet(2, false, 34, true);

  // printing the elements of the tree sub set
  System.out.println("***Elements of the treeSubSet where "
    + "fromElement exclusive and toElement inclusive***");
  for (Integer val : treeSubSet) {
   System.out.println(val);
  }

  // creating subset from tree set, fromElement and toElement
  // inclusive
  treeSubSet = (TreeSet<Integer>) treeSet.subSet(2, true, 34, true);

  // printing the elements of the tree sub set
  System.out.println("***Elements of the treeSubSet where "
    + "fromElement inclusive and toElement inclusive***");
  for (Integer val : treeSubSet) {
   System.out.println(val);
  }
 }
}

Output of above program is shown below:

***Elements of the treeSet***
2
13
23
28
34
***Elements of the treeSubSet where fromElement exclusive and toElement inclusive***
13
23
28
34
***Elements of the treeSubSet where fromElement inclusive and toElement inclusive***
2
13
23
28
34

java.util.TreeSet.subSet(E fromElement, boolean fromInclusive, E toElement, boolean toInclusive) with ConcurrentModificationException

The subSet(E fromElement, boolean fromInclusive, E toElement, boolean toInclusive) method returns a view of the portion of this set whose elements range from fromElement to toElement. The returned set is backed by this set, so changes in the returned set are reflected in this set, and vice-versa. while iterating over set if either set is modified then ConcurrentModificationException. In below program, you can see while iterating over set using for-each, any modification in either set will throw ConcurrentModificationException.

You can also see the code using the TreeSet's iterator() method by uncommenting in below code and comment for-each part. Once you create an iterator over either set, any modification on either set while iterating over set will throw ConcurrentModificationException.

package com.walking.techie;

import java.util.Iterator;
import java.util.TreeSet;

public class TreeSetSubSetConcurrentModificationException {

 public static void main(String[] args) {
  // create an empty tree set
  TreeSet<Integer> treeSet = new TreeSet<Integer>();
  treeSet.add(23);
  treeSet.add(34);
  treeSet.add(2);
  treeSet.add(21);
  treeSet.add(45);
  treeSet.add(15);

  TreeSet<Integer> treeSubSet = (TreeSet<Integer>) treeSet.subSet(2,true, 28,false);

  // printing the elements of tree set
  System.out.println("***Elements of the treeSet***");
  System.out.println(treeSet);
  System.out.println("***Elements of the tree sub set***");
  System.out.println(treeSubSet);

  for (Integer val : treeSet) {
   if (val == 21) {
    treeSubSet.add(20);// ConcurrentModificationException
   }
  }

  // create an iterator of tree sub set
  /*Iterator<Integer> iterator = treeSubSet.iterator();
  while (iterator.hasNext()) {
   Integer val = iterator.next();
   if (val == 21) {
    treeSet.add(40); // ConcurrentModificationException
   }
  }*/
 }
}

Output of above program is shown below:

***Elements of the treeSet***
[2, 15, 21, 23, 34, 45]
***Elements of the tree sub set***
[2, 15, 21, 23]
Exception in thread "main" java.util.ConcurrentModificationException
 at java.util.TreeMap$NavigableSubMap$SubMapIterator.nextEntry(TreeMap.java:1703)
 at java.util.TreeMap$NavigableSubMap$SubMapKeyIterator.next(TreeMap.java:1780)
 at com.walking.techie.TreeSetSubSetConcurrentModificationException.main(TreeSetSubSetConcurrentModificationException.java:35)

java.util.TreeSet.subSet(E fromElement, boolean fromInclusive, E toElement, boolean toInclusive) with IllegalArgumentException

The tree sub set returned by subSet(E fromElement, boolean fromInclusive, E toElement, boolean toInclusive) method call, and modified the tree sub set 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, then it will throw IllegalArgumentException.

package com.walking.techie;

import java.util.TreeSet;

public class TreeSetSubSetIllegalArgumentException2 {

 public static void main(String[] args) {
  // create an empty tree set
  TreeSet<Integer> treeSet = new TreeSet<Integer>();
  treeSet.add(23);
  treeSet.add(34);
  treeSet.add(2);
  treeSet.add(21);
  treeSet.add(45);
  treeSet.add(15);

  TreeSet<Integer> treeSubSet = (TreeSet<Integer>) treeSet.subSet(2, true, 28, false);

  // printing the elements of tree set
  System.out.println("***Elements of the treeSet***");
  System.out.println(treeSet);
  System.out.println("***Elements of the tree sub set***");
  System.out.println(treeSubSet);

  for (Integer val : treeSet) {
   if (val == 21) {
    treeSubSet.add(30);// IllegalArgumentException
   }
  }

  /*for (Integer val : treeSubSet) {
   if (val == 21) {
    treeSubSet.add(30);// IllegalArgumentException
   }
  }*/
 }
}

Output of above program is shown below:

***Elements of the treeSet***
[2, 15, 21, 23, 34, 45]
***Elements of the tree sub set***
[2, 15, 21, 23]
Exception in thread "main" java.lang.IllegalArgumentException: key out of range
 at java.util.TreeMap$NavigableSubMap.put(TreeMap.java:1516)
 at java.util.TreeSet.add(TreeSet.java:255)
 at com.walking.techie.TreeSetSubSetIllegalArgumentException2.main(TreeSetSubSetIllegalArgumentException2.java:27)

No comments :

Post a Comment