Java TreeSet ceiling(E e) Example

The ceiling(E e) method of java.util.TreeSet class will returns the least element in the set greater than or equal to the given element, or null if there is no such element.

Signature

public E ceiling(E e)

This method returns the least element in the set greater than or equal to the given element, or null if there is no such element. and will throw java.lang.NullPointerException when the specified collection is null and java.lang.ClassCastException if the elements provided cannot be compared with the elements currently in the set

Example

The following example shows the usage of java.util.TreeSet.ceiling(e) method.

import java.util.TreeSet;

public class CeilingMethodTreeSetExample 
{
    public static void main(String args[])
    {
        //Create TreeSet object treeSet1
        TreeSet treeSet1 = new TreeSet();
        
        //add the elements to treeSet1 using add() method
        treeSet1.add(1);
        treeSet1.add(2);
        treeSet1.add(4);
        treeSet1.add(5);
        
        System.out.println("Ceiling of \"3\" is : "+treeSet1.ceiling(3));
    }
}

Output

Ceiling of "3" is : 4

Leave a Reply

Your email address will not be published. Required fields are marked *