Java TreeSet lower() Method

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

Definition

In Java, lower(e) returns the greatest element strictly less than e, or null if there is no smaller element in the TreeSet.

Unlike floor(e), lower(e) never returns e itself: even when e exists in the set, lower(e) skips over it to find the immediate predecessor.

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 reference value to retrieve the element immediately below it.

Java
NavigableSet<Integer> scores = new TreeSet<>(Arrays.asList(100, 200, 300));
Integer prev = scores.lower(200);    // 100 (strictly < 200)
Integer none = scores.lower(100);    // null (nothing < 100)
CallWhat it does
set.lower(e)Greatest element strictly < e, or null.
set.floor(e)Greatest element <= e (includes e if present).
set.higher(e)Opposite direction: least element strictly > e.
set.headSet(e, false)All elements strictly less than e.

When iterating backward from a cutoff point without creating a view, starting at lower(cutoff) and stepping via lower(cur) is a clean O(log n)-per-step pattern.

Library declarations
Java
// java.util.TreeSet<E>  (since Java 6)
public E lower(E e)

// Declared in NavigableSet<E>

Parameters

ParameterDescription
e (E)The exclusive upper reference value.

Return Value

Return ValueDescription
EThe greatest element strictly < e.
nullIf the set is empty or e is less than or equal to the first element.

Compare with Similar Methods

Compare lower() with the other NavigableSet queries:

MethodHow it differs
floor(e)Allows an exact match (<= e) instead of strictly less.
higher(e)Finds the immediate successor strictly > e.
ceiling(e)Allows an exact match above (>= e).
headSet(e, false)Returns a live view of every element below e.

Java Examples

Example 1 — Find the Next Highest Player Score Strictly Below Yours

Even when your score is already in the leaderboard, lower() skips it and reveals the runner-up right below.

Java
import java.util.Arrays;
import java.util.NavigableSet;
import java.util.TreeSet;

public class TreeSetLowerRank {
    public static void main(String[] args) {
        NavigableSet<Integer> leaderboard = new TreeSet<>(Arrays.asList(1200, 1450, 1800, 2100));
        int myScore = 1800;

        Integer rightBelow = leaderboard.lower(myScore);
        Integer atOrBelow = leaderboard.floor(myScore);

        System.out.println("score right below 1800: " + rightBelow);
        System.out.println("floor(1800) for comparison: " + atOrBelow);
    }
}

How It Works

  1. Exact value skipped. 1800 is in the leaderboard, yet lower(1800) demands strictly smaller values and returns 1450.
  2. Contrast with floor(). floor(1800) accepts equality and returns 1800 itself.
  3. Between two scores works too. Passing 1700 instead would also return 1450.

Example 2 — Step Backward Through Sorted Release Tags

Start from a target version string and walk backward through earlier releases using repeated lower() calls.

Java
import java.util.Arrays;
import java.util.NavigableSet;
import java.util.TreeSet;

public class TreeSetLowerReleases {
    public static void main(String[] args) {
        NavigableSet<String> releases = new TreeSet<>(Arrays.asList("v1.0", "v1.2", "v1.5", "v2.0", "v2.1"));

        String cur = "v2.0";
        System.out.println("releases before " + cur + ":");
        while ((cur = releases.lower(cur)) != null) {
            System.out.println("- " + cur);
        }
    }
}

How It Works

  1. Strict predecessor jump. Starting at "v2.0", the first lower("v2.0") moves directly to "v1.5".
  2. Walking backward step by step. Passing each returned tag back into lower() yields "v1.2" then "v1.0".
  3. Clean stop at the minimum. Calling lower("v1.0") returns null because nothing precedes the first release.

Example 3 — Boundary Behavior at first() and Wrong Type Error

Verify that lower(set.first()) is null and see ClassCastException on an incompatible key.

Java
import java.util.Arrays;
import java.util.NavigableSet;
import java.util.TreeSet;

public class TreeSetLowerBoundary {
    public static void main(String[] args) {
        NavigableSet<Integer> prices = new TreeSet<>(Arrays.asList(10, 20, 30));

        System.out.println("lower than first (10): " + prices.lower(prices.first()));
        System.out.println("lower than 5:          " + prices.lower(5));

        try {
            ((NavigableSet) prices).lower("ten");
        } catch (ClassCastException e) {
            System.out.println("wrong type: " + e.getClass().getSimpleName());
        }
    }
}

How It Works

  1. Nothing before the minimum. Even though 10 is inside prices, no stored number is strictly less than 10, so lower(10) returns null.
  2. Values below minimum return null too. 5 sits below every element, producing null safely.
  3. Comparable types required. Comparing "ten" against Integer nodes fails with ClassCastException.

Frequently Asked Questions

The greatest element in the TreeSet that is strictly less than e (< e), or null if no smaller element exists.
lower(e) is strictly less (< e) and skips e even if e is present. floor(e) is less than or equal (<= e) and returns e if present.
null, because no element in the set is strictly smaller than the first element.
No. It only looks up the element in O(log n) time and leaves the TreeSet unchanged.
Java 6 (JDK 1.6), as part of NavigableSet.

Next: pollFirst()

Remove and return the smallest element, or null if the set is empty.

pollFirst() 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.

13 people found this page helpful