Java TreeSet higher() Method
Example
Find the number that comes just after a given number:
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.higher(30));
System.out.println(numbers.higher(40));
}
}
Definition and Usage
The higher() method returns the least (lowest) item in the set that is greater than the specified value.
Unlike ceiling(), it never returns the value itself. If there is no greater item, the method returns null.
Tip: Use the lower() method to find the item that comes just before a given value.
Syntax
public T higher(T item)
T refers to the data type of items in the set.
Parameter Values
| Parameter | Description |
|---|---|
| item | Required. The value to compare with. It does not have to be in the set. |
Technical Details
| Returns: | The least item that is greater than item, or null if there is no such item. |
|---|
Related Pages
❮ TreeSet Methods