
- Java.util Package Classes
- Java.util - Home
- Java.util - ArrayDeque
- Java.util - ArrayList
- Java.util - Arrays
- Java.util - BitSet
- Java.util - Calendar
- Java.util - Collections
- Java.util - Currency
- Java.util - Date
- Java.util - Dictionary
- Java.util - EnumMap
- Java.util - EnumSet
- Java.util - Formatter
- Java.util - GregorianCalendar
- Java.util - HashMap
- Java.util - HashSet
- Java.util - Hashtable
- Java.util - IdentityHashMap
- Java.util - LinkedHashMap
- Java.util - LinkedHashSet
- Java.util - LinkedList
- Java.util - ListResourceBundle
- Java.util - Locale
- Java.util - Observable
- Java.util - PriorityQueue
- Java.util - Properties
- Java.util - PropertyPermission
- Java.util - PropertyResourceBundle
- Java.util - Random
- Java.util - ResourceBundle
- Java.util - ResourceBundle.Control
- Java.util - Scanner
- Java.util - ServiceLoader
- Java.util - SimpleTimeZone
- Java.util - Stack
- Java.util - StringTokenizer
- Java.util - Timer
- Java.util - TimerTask
- Java.util - TimeZone
- Java.util - TreeMap
- Java.util - TreeSet
- Java.util - UUID
- Java.util - Vector
- Java.util - WeakHashMap
- Java.util Package Extras
- Java.util - Interfaces
- Java.util - Exceptions
- Java.util - Enumerations
- Java.util Useful Resources
- Java.util - Useful Resources
- Java.util - Discussion
Java TreeSet tailSet() Method
Description
The Java TreeSet tailSet(E fromElement) method is used to return a view of the portion of this set whose elements are greater than or equal to fromElement.
Declaration
Following is the declaration for java.util.TreeSet.tailSet() method.
public SortedSet<E> tailSet(E fromElement)
Parameters
fromElement − This is the low endpoint (inclusive) of the returned set.
Return Value
The method call returns a view of the portion of this set whose elements are greater than or equal to fromElement.
Exception
ClassCastException − This is thrown if fromElement is not compatible with this set's comparator (or, if the set has no comparator, if fromElement does not implement Comparable). Implementations may, but are not required to, throw this exception if fromElement cannot be compared to elements currently in the set.
NullPointerException − This is thrown if fromElement is null and this set uses natural ordering, or its comparator does not permit null elements
IllegalArgumentException − This is thrown if this set itself has a restricted range, and fromElement lies outside the bounds of the range
Java TreeSet tailSet(E fromElement,boolean fromInclusive,E toElement,boolean toInclusive) Method
Description
The tailSet(E fromElement,boolean fromInclusive,E toElement,boolean toInclusive) method is used to return 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 fromExclusive and toExclusive are both true.
Declaration
Following is the declaration for java.util.TreeSet.tailSet() method.
public NavigableSet<E> tailSet(E fromElement, boolean inclusive)
Parameters
fromElement − This is the low endpoint of the returned set.
inclusive − This is true if the low endpoint is to be included in the returned view.
Return Value
The method call returns a view of the portion of this set whose elements are greater than or equal to fromElement.
Exception
ClassCastException − This is thrown if fromElement is not compatible with this set's comparator (or, if the set has no comparator, if fromElement does not implement Comparable). Implementations may, but are not required to, throw this exception if fromElement cannot be compared to elements currently in the set.
NullPointerException − This is thrown if fromElement is null and this set uses natural ordering, or its comparator does not permit null elements
IllegalArgumentException − This is thrown if this set itself has a restricted range, and fromElement lies outside the bounds of the range
Getting tailSet of a TreeSet of Integer Example
The following example shows the usage of Java TreeSet tailSet(E fromElement) method to get a view of the portion of this set whose elements range from fromElement, inclusive. We've created two TreeSet objects of Integer. Then few entries are added using add() method and a tailSet retrieved from first treeset and printed.
package com.tutorialspoint; import java.util.TreeSet; public class TreeSetDemo { public static void main(String[] args) { // creating a TreeSet TreeSet <Integer>treeadd = new TreeSet<>(); TreeSet <Integer>treetailSet = new TreeSet<>(); // adding in the tree set treeadd.add(1); treeadd.add(2); treeadd.add(3); treeadd.add(4); treeadd.add(5); treeadd.add(6); treeadd.add(7); treeadd.add(8); // creating tailSet treetailSet = (TreeSet)treeadd.tailSet(3); System.out.println("Tree tailSet data: " + treetailSet); } }
Output
Let us compile and run the above program, this will produce the following result.
Tree tailSet data: [3, 4, 5, 6, 7, 8]
Getting tailSet of a TreeSet of String Example
The following example shows the usage of Java TreeSet tailSet(E fromElement,boolean inclusive) method to get a view of the portion of this set whose elements range from fromElement (included as inclusive is true). We've created two TreeSet objects of Integer. Then few entries are added using add() method and a tailSet retrieved from first treeset and printed.
package com.tutorialspoint; import java.util.TreeSet; public class TreeSetDemo { public static void main(String[] args) { // creating a TreeSet TreeSet <Integer>treeadd = new TreeSet<>(); TreeSet <Integer>treetailSet = new TreeSet<>(); // adding in the tree set treeadd.add(1); treeadd.add(2); treeadd.add(3); treeadd.add(4); treeadd.add(5); treeadd.add(6); treeadd.add(7); treeadd.add(8); // creating tailSet treetailSet = (TreeSet)treeadd.tailSet(3,true); System.out.println("Tree tailSet data: " + treetailSet); } }
Output
Let us compile and run the above program, this will produce the following result.
Tree tailSet data: [3, 4, 5, 6, 7, 8]
Getting tailSet of a TreeSet of Object Example
The following example shows the usage of Java TreeSet tailSet(E fromElement,boolean inclusive) method to get a view of the portion of this set whose elements range from fromElement (excluded as fromInclusive is false). We've created two TreeSet objects of Integer. Then few entries are added using add() method and a tailSet retrieved from first treeset and printed.
package com.tutorialspoint; import java.util.TreeSet; public class TreeSetDemo { public static void main(String[] args) { // creating a TreeSet TreeSet <Integer>treeadd = new TreeSet<>(); TreeSet <Integer>treetailSet = new TreeSet<>(); // adding in the tree set treeadd.add(1); treeadd.add(2); treeadd.add(3); treeadd.add(4); treeadd.add(5); treeadd.add(6); treeadd.add(7); treeadd.add(8); // creating tailSet treetailSet = (TreeSet)treeadd.tailSet(3,false); System.out.println("Tree tailSet data: " + treetailSet); } }
Output
Let us compile and run the above program, this will produce the following result.
Tree tailSet data: [4, 5, 6, 7, 8]