Java TreeSet tailSet() Method
Example
Get the numbers from 30 to the end, first with and then without 30 itself:
import java.util.TreeSet;
public class Main {
public static void main(String[] args) {
TreeSet<Integer> numbers = new TreeSet<Integer>();
numbers.add(40);
numbers.add(10);
numbers.add(30);
numbers.add(20);
System.out.println(numbers.tailSet(30));
System.out.println(numbers.tailSet(30, false));
}
}
Definition and Usage
The tailSet() method returns the part of the set with the items that are greater than or equal to the specified value. By default, the value itself is included. Set the second parameter to false to leave it out.
The returned set is a view of the original set (it shows the same items, it is not a copy). If you change one of them, the other one changes too.
Tip: Use the headSet() method to get the items before a given value.
Syntax
One of the following:
public SortedSet<T> tailSet(T fromItem)
public NavigableSet<T> tailSet(T fromItem, boolean inclusive)
T refers to the data type of items in the set.
Parameter Values
| Parameter | Description |
|---|---|
| fromItem | Required. The returned set has the items that are greater than or equal to this value. It does not have to be in the set. |
| inclusive | Optional. If false, the returned set does not have fromItem. The default is true. |
Technical Details
| Returns: | A set with the items that are greater than or equal to fromItem (only greater than, if inclusive is false). |
|---|
Related Pages
❮ TreeSet Methods