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, boolean inclusive)
The headSet(E toElement, boolean inclusive) method returns a view of the portion of this set whose elements are less than (or equal to, if inclusive is true) 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, boolean inclusive) method.
- toElement -> High endpoint of the returned set.
- inclusive -> 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 are less than (or equal to, if inclusive is true) 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, boolean inclusive)
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 28 TreeSet<Integer> treeHeadSet = (TreeSet<Integer>) treeSet.headSet(28, false); // printing the elements of the treeHeadSet System.out.println("***Elements of the treeHeadSet less than 28 ***"); for (Integer val : treeHeadSet) { System.out.println(val); } treeHeadSet = (TreeSet<Integer>) treeSet.headSet(28, true); // printing the elements of the treeHeadSet System.out.println("***Elements of the treeHeadSet " + "less than or equal to 28 ***"); 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 treeHeadSet less than 28 *** 2 13 23 ***Elements of the treeHeadSet less than or equal to 28 *** 2 13 23 28
java.util.TreeSet.headSet(E toElement, boolean inclusive) with ConcurrentModificationException
The headSet(E toElement, boolean inclusive) method returns a view of the portion of this set whose elements are less than (or equal to, if inclusive is true) 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.TreeSet; import java.util.Iterator; public class TreeSetHeadSetConcurrentModificationException { 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 or equal to 28 TreeSet<Integer> treeHeadSet = (TreeSet<Integer>) treeSet.headSet(28); 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.TreeSetHeadSetConcurrentModificationException.main(TreeSetHeadSetConcurrentModificationException.java:31)
java.util.TreeSet.headSet(E toElement, boolean inclusive) with IllegalArgumentException
The set returned by headSet(E toElement, boolean inclusive) 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, true); // 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); // 28 is less than or equal to 28 (key boundary) treeHeadSet.add(28);//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(29);//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, 28] 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:32)
factorial hundred In the last few days, the “factorial of 100” is one of the top subjects and a lot of maths geeks compute it using voice assistants such as Alexa, Shiri, etc.
ReplyDeletefactorial hundred In the last few days, the “factorial of 100” is one of the top subjects and a lot of maths geeks compute it using voice assistants such as Alexa, Shiri, etc.
factorial hundred In the last few days, the “factorial of 100” is one of the top subjects and a lot of maths geeks compute it using voice assistants such as Alexa, Shiri, etc.