The java.util.TreeSet.subSet() is used to return a subset of the existing TreeSet within a range mentioned in the parameter. The method takes in an upper limit and a lower limit and returns all the elements mentioned in the range. The lower limit is included if the element is present within the set and the upper limit is excluded. Basically, it takes the subset greater than equal to the lower limit and strictly less than the upper element.
TreeSet tree_set.subSet(Object low_element, Object up_element)
- low_element: This is of the type of the TreeSet and defines the lower limit or the starting element from which the subset is evaluated. This element is included in the subset.
- up_element: This is of the type of the TreeSet and defines the upper limit or the last element up to which the subset is evaluated. This element is excluded from the subset.
Return Values: The method returns a subset of the TreeSet type mentioned within the given range of the parameters.
Below programs illustrate the java.util.TreeSet.subSet() method:
The resultant values within the sub set: 6 9 10 18 20
The resultant values within the sub set: 4 Geek Geeks To
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.