In this example, We will discuss about how to get the tail set (sub set) from java TreeSet object.
Get Tail Set from Java TreeSet example
In this example, We will discuss about how to get the tail set (sub set) from java TreeSet object. the tail set containing the values grater than or equal to the specified value using tailSet() method of Java TreeSet class. To get the tail set we use SortedSet tailSet(Object fromElement) method. This method returns a view of the portion of this set whose elements are greater than or equal to fromElement.
The Example is as follows-
tailSetExample.java
package devmanuals.com; import java.util.*; public class tailSetExample { public static void main(String args[]) { TreeSet oTreeSet = new TreeSet(); oTreeSet.add("C"); oTreeSet.add("A"); oTreeSet.add("B"); oTreeSet.add("E"); oTreeSet.add("F"); oTreeSet.add("D"); System.out.println("The elements are: " + oTreeSet); SortedSet Osort = oTreeSet.tailSet("C"); System.out.println("The tail subset of TreeSet is: " + Osort); } }
Output:-
The elements are: [A, B, C, D, E, F]
The tail subset of TreeSet is: [C, D, E, F] |
[ 0 ] Comments