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 headSet(E toElement)
The headSet(E toElement) method returns a view of the portion of this set whose elements are strictly less than toElement. 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.headSet(E toElement) method.
- toElement -> High endpoint (exclusive) of the returned set.
Return value : Returns a view of the portion of this set whose elements are strictly less than toElement.
Exception :- ClassCastException -> If toElement is not compatible with this set's comparator (or, if the set has no comparator, if toElement does not implement Comparable).
- NullPointerException -> If toElement is null and this set uses natural ordering, or its comparator does not permit null elements.
- IllegalArgumentException -> If this set itself has a restricted range, and toElement lies outside the bounds of the range.
Simple example of TreeSet.headSet(E toElement)
The following example shows the usage of java.util.TreeSet.headSet() method.
package com.walking.techie; import java.util.TreeSet; public class TreeSetHeadSetDemo { 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 set whose values less than 25 TreeSet<Integer> treeHeadSet = (TreeSet<Integer>) treeSet.headSet(25); // printing the elements of the treeHeadSet System.out.println("***Elements of the tree head set ***"); for (Integer val : treeHeadSet) { System.out.println(val); } } }
Output of above program is shown below:
***Elements of the treeSet*** 2 13 23 28 34 ***Elements of the tree head set *** 2 13 23
java.util.TreeSet.headSet(E toElement) with ConcurrentModificationException
The headSet(E toElement) method returns a view of the portion of this set whose elements are strictly less than toElement. The returned is backed by this set, 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 TreeSetHeadSetException { 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); // 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 set whose values less than 25 TreeSet<Integer> treeHeadSet = (TreeSet<Integer>) treeSet.headSet(25); System.out.println("***Elements of the tree head set***"); System.out.println(treeHeadSet); for (Integer val : treeSet) { if (val == 21) { treeHeadSet.add(20);// ConcurrentModificationException } } // create an iterator of treeHeadSet /*Iterator<Integer> iterator = treeHeadSet.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 head 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.TreeSetHeadSetException.main(TreeSetHeadSetException.java:30)
java.util.TreeSet.headSet(E toElement) with IllegalArgumentException
The set returned by headSet(E toElement) method call, and modified the returned set, If this set itself has a restricted range, and toElement lies outside the bounds of the range, then it will throw IllegalArgumentException.
package com.walking.techie; import java.util.TreeSet; public class TreeSetHeadSetIllegalArgumentException { 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> treeHeadSet = (TreeSet<Integer>) treeSet.headSet(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 head set***"); System.out.println(treeHeadSet); // 27 is less than 28 (key boundary) treeHeadSet.add(27);//not throw IllegalArgumentException System.out.println("***Elements of the modified tree head set***"); System.out.println(treeHeadSet); //adding element should be less than key boundary treeHeadSet.add(28);//throw IllegalArgumentException System.out.println("***Elements of the modified tree head set***"); System.out.println(treeHeadSet); } }
Output of above program is shown below:
***Elements of the treeSet*** [2, 15, 21, 23, 34, 45] ***Elements of the tree head set*** [2, 15, 21, 23] ***Elements of the modified tree head set*** [2, 15, 21, 23, 27] 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.TreeSetHeadSetIllegalArgumentException.main(TreeSetHeadSetIllegalArgumentException.java:31)
No comments :
Post a Comment