Java TreeSet lower() Method
Example
Find the number that comes just before 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.lower(30));
System.out.println(numbers.lower(10));
}
}
Definition and Usage
The lower() method returns the greatest item in the set that is less than the specified value.
Unlike floor(), it never returns the value itself. If there is no smaller item, the method returns null.
Tip: Use the higher() method to find the item that comes just after a given value.
Syntax
public T lower(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 item, or null if there is no such item. |
|---|
Related Pages
❮ TreeSet Methods