Java TreeSet floor() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Java 6+

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.

Java
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)
CallWhat 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
// java.util.TreeSet<E>  (since Java 6)
public E floor(E e)

// Declared in NavigableSet<E>

Parameters

ParameterDescription
e (E)The upper target value to match against. Cannot be null under natural ordering.

Return Value

Return ValueDescription
EThe greatest element <= e according to the set's ordering.
nullIf the set is empty or all elements are strictly greater than e.

Compare with Similar Methods

All four neighbour navigation methods side by side:

MethodHow 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

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.

Java
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));
    }
}

How It Works

  1. Exact hit preserved. Because 42 exists in stocked, floor(42) returns 42 immediately.
  2. Step down on miss. Size 41 is missing, so floor(41) returns 40, the largest stocked size still <= 41.
  3. 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.

Java
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));
        }
    }
}

How It Works

  1. Below first milestone. At $35, no threshold is <= 35, so tier is null and prints "none".
  2. Threshold boundary inclusive. Spending exactly $100 matches the $100 milestone thanks to <= semantics.
  3. 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.

Java
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());
        }
    }
}

How It Works

  1. Inclusive vs strict. When "C" exists in the set, floor("C") stops at "C" while lower("C") drops strictly below it to "B".
  2. Null disallowed by natural ordering. Passing null to grades.floor(null) triggers NullPointerException during tree comparison.
  3. Works since Java 6. Both floor() and lower() come from NavigableSet.

Frequently Asked Questions

It returns the greatest element in the TreeSet that is less than or equal to e, or null if no such element exists.
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).
No. It only searches the tree and leaves all elements untouched.
floor(e) returns null. Store the result in an object variable like Integer rather than unboxed int.
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

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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