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