Wednesday, August 27, 2025
HomeLanguagesJavaTreeSet ceiling() method in Java with Examples

TreeSet ceiling() method in Java with Examples

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:




// Java program to demonstrate
// ceiling() method
  
import java.util.*;
  
public class GFG1 {
    public static void main(String[] argv)
        throws Exception
    {
  
        try {
  
            // create tree set object
            TreeSet<Integer> treeadd = new TreeSet<Integer>();
  
            // populate the TreeSet
            treeadd.add(10);
            treeadd.add(20);
            treeadd.add(30);
            treeadd.add(40);
  
            // Print the TreeSet
            System.out.println("TreeSet: " + treeadd);
  
            // getting ceiling value for 25
            // using ceiling() method
            int value = treeadd.ceiling(25);
  
            // printing  the ceiling value
            System.out.println("Ceiling value for 25: "
                               + value);
        }
  
        catch (NullPointerException e) {
            System.out.println("Exception thrown : " + e);
        }
    }
}


Output:

TreeSet: [10, 20, 30, 40]
Ceiling value for 25: 30

Example 2: To demonstrate NullPointerException.




// Java program to demonstrate
// ceiling() method for NullPointerException
  
import java.util.*;
  
public class GFG1 {
    public static void main(String[] argv)
        throws Exception
    {
  
        try {
  
            // create tree set object
            TreeSet<Integer> treeadd = new TreeSet<Integer>();
  
            // populate the TreeSet
            treeadd.add(10);
            treeadd.add(20);
            treeadd.add(30);
            treeadd.add(40);
  
            // Print the TreeSet
            System.out.println("TreeSet: " + treeadd);
  
            // getting ceiling value for null
            // using ceiling() method
            System.out.println("Trying to compare"
                               + " with null value ");
  
            int value = treeadd.ceiling(null);
  
            // printing  the ceiling value
            System.out.println("Ceiling value for null: " + value);
        }
  
        catch (NullPointerException e) {
            System.out.println("Exception: " + e);
        }
    }
}


Output:

TreeSet: [10, 20, 30, 40]
Trying to compare with null value 
Exception: java.lang.NullPointerException
Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

1 COMMENT

Most Popular

Dominic
32236 POSTS0 COMMENTS
Milvus
80 POSTS0 COMMENTS
Nango Kala
6609 POSTS0 COMMENTS
Nicole Veronica
11779 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11828 POSTS0 COMMENTS
Shaida Kate Naidoo
6719 POSTS0 COMMENTS
Ted Musemwa
7002 POSTS0 COMMENTS
Thapelo Manthata
6678 POSTS0 COMMENTS
Umr Jansen
6690 POSTS0 COMMENTS