VOOZH about

URL: https://www.geeksforgeeks.org/java/treeset-ceiling-method-in-java-with-examples/

⇱ TreeSet ceiling() method in Java with Examples - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

TreeSet ceiling() method in Java with Examples

Last Updated : 11 Jul, 2025
The ceiling() method of java.util.TreeSet<E> class is used to return the least element in this set greater than or equal to the given element, or null if there is no such element. Syntax:
public E ceiling(E e)
Parameters: This method takes the value e as a parameter which is to be matched. Return Value: This method returns the least element greater than or equal to e, or null if there is no such element. Exception: This method throws NullPointerException if the specified element is null and this set uses natural ordering, or its comparator does not permit null elements. Below are the examples to illustrate the ceiling() method Example 1:
Output:
TreeSet: [10, 20, 30, 40]
Ceiling value for 25: 30
Example 2: To demonstrate NullPointerException.
Output:
TreeSet: [10, 20, 30, 40]
Trying to compare with null value 
Exception: java.lang.NullPointerException
Comment