Home Java TreeSet Methods floor() Java TreeSet floor() Method Definition In Java, floor(e) returns the greatest element in the set that is less than or equal to e , or null if every element in the set is larger than e.
Notice the "or equal to" part: if e itself is in the set, floor(e) returns e. If e is missing, it steps down to the nearest value below e in O(log n) time.
An instance method of java.util.TreeSet (Java 6+ (added via NavigableSet in JDK 1.6)): a red-black tree NavigableSet backed by a TreeMap that keeps unique elements sorted in O(log n) time and is not thread-safe .
Syntax Pass a target value to find the highest match at or below it.
NavigableSet<Integer> sizes = new TreeSet<>(Arrays.asList(38, 40, 42, 44));
Integer exact = sizes.floor(40); // 40 (exact match)
Integer fit = sizes.floor(41); // 40 (largest <= 41)
Integer none = sizes.floor(36); // null (nothing <= 36) Call What it does set.floor(e)Greatest element <= e, or null. set.lower(e)Strictly less (< e); skips e even if present. set.ceiling(e)Opposite direction: least element >= e. set.floor(target) != nullChecks whether any qualifying entry exists at or below target.
Think of floor as rounding a lookup key down onto the closest existing value in the set (including staying put on an exact hit).
Library declarations // java.util.TreeSet<E> (since Java 6)
public E floor(E e)
// Declared in NavigableSet<E> Parameters Parameter Description e (E)The upper target value to match against. Cannot be null under natural ordering.
Return Value Return Value Description EThe greatest element <= e according to the set's ordering. nullIf the set is empty or all elements are strictly greater than e.
Important
Store the result in a wrapper type like Integer before checking for null, because unboxing null into a primitive int throws NullPointerException. Passing null with natural ordering throws NullPointerException when elements are present (as documented by NavigableSet), and incompatible types throw ClassCastException.
Compare with Similar Methods All four neighbour navigation methods side by side:
Method How it differs lower(e)Strictly below: greatest element < e (excludes e itself). ceiling(e)At or above: least element >= e. higher(e)Strictly above: least element > e. contains(e)Returns only true/false for an exact match without finding neighbours.
Java Examples 1. Clothing size bracket 2. Discount spend tiers 3. floor vs lower and null Example 1 — Pick the Largest Available Shoe Size Up to a Limit Match customer foot measurements against stocked sizes using floor() so exact fits stay exact and in-between values step down.
import java.util.Arrays;
import java.util.NavigableSet;
import java.util.TreeSet;
public class TreeSetFloorSizes {
public static void main(String[] args) {
NavigableSet<Integer> stocked = new TreeSet<>(Arrays.asList(38, 40, 42, 44));
System.out.println("target 42 -> " + stocked.floor(42));
System.out.println("target 41 -> " + stocked.floor(41));
System.out.println("target 36 -> " + stocked.floor(36));
}
} target 42 -> 42
target 41 -> 40
target 36 -> null How It Works Exact hit preserved. Because 42 exists in stocked, floor(42) returns 42 immediately.Step down on miss. Size 41 is missing, so floor(41) returns 40, the largest stocked size still <= 41.No match below minimum. Every size in the set exceeds 36, so floor(36) returns null.Example 2 — Look Up Which Reward Tier an Order Qualifies For Find the highest spend milestone reached by each shopping cart total without an if-else chain.
import java.util.Arrays;
import java.util.NavigableSet;
import java.util.TreeSet;
public class TreeSetFloorTiers {
public static void main(String[] args) {
NavigableSet<Integer> milestones = new TreeSet<>(Arrays.asList(50, 100, 250, 500));
int[] carts = {35, 100, 185, 620};
for (int total : carts) {
Integer tier = milestones.floor(total);
System.out.println("cart $" + total + " -> tier " + (tier == null ? "none" : "$" + tier));
}
}
} cart $35 -> tier none
cart $100 -> tier $100
cart $185 -> tier $100
cart $620 -> tier $500 How It Works Below first milestone. At $35, no threshold is <= 35, so tier is null and prints "none".Threshold boundary inclusive. Spending exactly $100 matches the $100 milestone thanks to <= semantics.Capped at top tier. Spending $620 qualifies for the highest tier available, returning $500.Example 3 — Compare floor() vs lower() and Catch NullPointerException See the difference when the key is already in the set, then pass null on a non-empty natural-order set.
import java.util.Arrays;
import java.util.NavigableSet;
import java.util.TreeSet;
public class TreeSetFloorVsLower {
public static void main(String[] args) {
NavigableSet<String> grades = new TreeSet<>(Arrays.asList("A", "B", "C", "D"));
System.out.println("floor(\"C\"): " + grades.floor("C"));
System.out.println("lower(\"C\"): " + grades.lower("C"));
try {
grades.floor(null);
} catch (NullPointerException e) {
System.out.println("floor(null): " + e.getClass().getSimpleName());
}
}
} floor("C"): C
lower("C"): B
floor(null): NullPointerException How It Works Inclusive vs strict. When "C" exists in the set, floor("C") stops at "C" while lower("C") drops strictly below it to "B".Null disallowed by natural ordering. Passing null to grades.floor(null) triggers NullPointerException during tree comparison.Works since Java 6. Both floor() and lower() come from NavigableSet.Frequently Asked Questions What does TreeSet floor(e) return? It returns the greatest element in the TreeSet that is less than or equal to e, or null if no such element exists.
What is the difference between floor() and lower()? floor(e) can return e itself if e is in the set (<= e), while lower(e) always returns an element strictly smaller than e (< e).
Does floor() modify the TreeSet? No. It only searches the tree and leaves all elements untouched.
What happens if no element is smaller than or equal to e? floor(e) returns null. Store the result in an object variable like Integer rather than unboxed int.
Can floor() return null on a non-empty set? Yes, whenever the smallest element in the TreeSet is still strictly greater than the argument you passed.
Next: higher() Find the smallest element strictly greater than a target, or null.
higher() tutorial → About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
8 people found this page helpful
Helpful Share Copy link Suggestion