Java TreeSet ceiling() Method
Example
Find the closest number that is equal to or greater than 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.ceiling(25));
System.out.println(numbers.ceiling(20));
}
}
Definition and Usage
The ceiling() method returns the least (lowest) item in the set that is greater than or equal to the specified value.
The value does not have to be in the set. If it is in the set, the method returns that value. If all items are less than the value, the method returns null.
Tip: Use the floor() method to find the closest item that is equal to or less than a given value.
Syntax
public T ceiling(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 or equal to item, or null if there is no such item. |
|---|
Related Pages
❮ TreeSet Methods