Home Java TreeSet Methods lower() Java TreeSet lower() Method 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.
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) Call What 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.util.TreeSet<E> (since Java 6)
public E lower(E e)
// Declared in NavigableSet<E> Parameters Parameter Description e (E)The exclusive upper reference value.
Return Value Return Value Description 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:
Method How 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 1. Score strictly below 2. Previous release tag 3. Boundary null and ClassCast 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.
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);
}
} score right below 1800: 1450
floor(1800) for comparison: 1800 How It Works Exact value skipped. 1800 is in the leaderboard, yet lower(1800) demands strictly smaller values and returns 1450.Contrast with floor(). floor(1800) accepts equality and returns 1800 itself.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.
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);
}
}
} releases before v2.0:
- v1.5
- v1.2
- v1.0 How It Works Strict predecessor jump. Starting at "v2.0", the first lower("v2.0") moves directly to "v1.5".Walking backward step by step. Passing each returned tag back into lower() yields "v1.2" then "v1.0".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.
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());
}
}
} lower than first (10): null
lower than 5: null
wrong type: ClassCastException How It Works Nothing before the minimum. Even though 10 is inside prices, no stored number is strictly less than 10, so lower(10) returns null.Values below minimum return null too. 5 sits below every element, producing null safely.Comparable types required. Comparing "ten" against Integer nodes fails with ClassCastException.Frequently Asked Questions What does TreeSet lower(e) return? The greatest element in the TreeSet that is strictly less than e (< e), or null if no smaller element exists.
How does lower() differ from floor()? 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.
What does lower() return if you pass the smallest element in the set? null, because no element in the set is strictly smaller than the first element.
Does lower() remove the element it returns? No. It only looks up the element in O(log n) time and leaves the TreeSet unchanged.
Which Java version added lower()? Java 6 (JDK 1.6), as part of NavigableSet.
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.
13 people found this page helpful
Helpful Share Copy link Suggestion