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

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

Saturday, December 17, 2016

java.util.TreeSet.subSet(E fromElement, E toElement)

What is Java TreeSet

The 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, E toElement)

The subSet(E fromElement, E toElement) method returns a view of the portion of this set whose elements range from fromElement, inclusive, to toElement, exclusive. (If fromElement and toElement are equal, the returned set is empty.)

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, E toElement) method.

public SortedSet<E> subSet(E fromElement, E toElement)
Parameters :
  • fromElement -> Low endpoint (inclusive) of the returned set.
  • toElement -> High endpoint (exclusive) of the returned set.
Return value : Returns a view of the portion of this set whose elements range from fromElement, inclusive, to toElement, exclusive.
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, E toElement)

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

package com.walking.techie;

import java.util.TreeSet;

public class TreeSetSubSetDemo {

 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
  TreeSet<Integer> treeSubSet = (TreeSet<Integer>) treeSet.subSet(2, 34);

  // printing the elements of the treeSubSet
  System.out.println("***Elements of the treeSubSet***");
  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***
2
13
23
28

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

The subSet(E fromElement, E toElement) method returns a view of the portion of this set whose elements range from fromElement, inclusive, to toElement, exclusive. 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 TreeSetSubSetException {

 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, 28);

  // 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$PrivateEntryIterator.nextEntry(TreeMap.java:1211)
 at java.util.TreeMap$KeyIterator.next(TreeMap.java:1265)
 at com.walking.techie.TreeSetSubSetException.main(TreeSetSubSetException.java:26)

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

The tree sub set returned by subSet(E fromElement, E toElement) 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.

import java.util.TreeSet;

public class TreeSetSubSetIllegalArgumentException {

 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, 28);

  // 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.TreeSetSubSetIllegalArgumentException.main(TreeSetSubSetIllegalArgumentException.java:27)

No comments :

Post a Comment