Course contentsShow
← Java
Lesson 1096 of 2,87025. Set ImplementationsFree lesson

Higher and Ceiling: Finding Elements Above a Given Value

Using higher() to find the least element strictly greater than a value, and ceiling() for greater than or equal.

Higher and Ceiling: Finding Elements Above a Given Value

What you'll learn: How to use higher() and ceiling() methods to find elements in a NavigableSet that are greater than a specified value.

The Core Idea

Just as lower() and floor() help you find elements below a target value, higher() and ceiling() work in the opposite direction—they find elements above your target. The key difference between them is whether they include the target value itself:

  • higher(E e): Returns the least element strictly greater than e (excludes e itself)
  • ceiling(E e): Returns the least element greater than or equal to e (includes e if present)

Think of it like a staircase analogy: if you're standing on step 5, higher(5) finds the next step up (step 6), while ceiling(5) is okay with staying on step 5 if it exists.

When to Use Each

Use higher() when you need the next element strictly above your value—useful for finding the immediate successor or the next scheduled event.

Use ceiling() when the target value itself is acceptable—perfect for "find this element or the next available one" scenarios, like finding an available time slot at or after a requested time.

Both methods return null if no qualifying element exists in the set.

The Relationship

The relationship mirrors lower() and floor():

  • ceiling(e) can return e itself
  • higher(e) always skips over e
  • If e doesn't exist in the set, both methods return the same result (the next element above)

Key Takeaway: Use higher() to find the strictly next greater element, and ceiling() to find the least element that's greater than or equal to your target—ceiling() is inclusive, higher() is exclusive.