Sunday, November 23, 2025
HomeLanguagesJavaConcurrentSkipListSet ceiling() method in Java

ConcurrentSkipListSet ceiling() method in Java

The java.util.concurrent.ConcurrentSkipListSet.ceiling() method is an in-built function in Java which returns the least element in this set greater than or equal to the given element, or null if there is no such element.

Syntax:

 ConcurrentSkipListSet.ceiling(E e)

Parameters: The function accepts a single parameter e i.e. the element to match.

Return Value: The function returns the least element greater than or equal to e, or null if there is no such element.

Exception: The function shows the following exceptions:

  • ClassCastException – if the specified element cannot be compared with the elements currently in the set
  • NullPointerException – if the specified element is null
  • Below programs illustrate the ConcurrentSkipListSet.ceiling() method:

    Program 1: To find ceiling of a number.




    // Java Program Demonstrate ceiling()
    // method of ConcurrentSkipListSet 
      
    import java.util.concurrent.*;
      
    class ConcurrentSkipListSetCeilingExample1 {
        public static void main(String[] args)
        {
            // Creating a set object
            ConcurrentSkipListSet<Integer> Lset = 
                            new ConcurrentSkipListSet<Integer>();
      
            // Adding elements to this set
            Lset.add(45);
            Lset.add(72);
            Lset.add(31);
            Lset.add(13);
            Lset.add(89);
      
            // Printing elements of the set
            System.out.println("The set contains: ");
            for (Integer i : Lset)
                System.out.print(i + " ");
      
            // Ceiling of 35
            System.out.println("\nCeiling of 35: " + Lset.ceiling(35));
      
            // Ceiling of 100
            System.out.println("\nCeiling of 100: " + Lset.ceiling(100));
        }
    }

    
    
    Output:

    The set contains: 
    13 31 45 72 89 
    Ceiling of 35: 45
    
    Ceiling of 100: null
    

    Program 2: To show NullPointerException in ceiling().




    // Java Program Demonstrate ceiling()
    // method of ConcurrentSkipListSet
      
    import java.util.concurrent.*;
    import java.io.*;
      
    class ConcurrentSkipListSetCeilingExample2 {
        public static void main(String[] args) throws IOException
        {
            // Creating a set object
            ConcurrentSkipListSet<Integer> Lset = 
                                new ConcurrentSkipListSet<Integer>();
      
            // Adding elements to this set
            Lset.add(45);
            Lset.add(72);
            Lset.add(31);
            Lset.add(13);
            Lset.add(89);
      
            try {
                // Ceiling of null
                System.out.println("Ceiling of null: " + Lset.ceiling(null));
            }
      
            catch (Exception e) {
                System.out.println("Exception : " + e);
            }
        }
    }

    
    
    Output:

    Exception : java.lang.NullPointerException
    

    Reference : https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ConcurrentSkipListSet.html#ceiling(E)

    RELATED ARTICLES

    Most Popular

    Dominic
    32407 POSTS0 COMMENTS
    Milvus
    97 POSTS0 COMMENTS
    Nango Kala
    6785 POSTS0 COMMENTS
    Nicole Veronica
    11932 POSTS0 COMMENTS
    Nokonwaba Nkukhwana
    12000 POSTS0 COMMENTS
    Shaida Kate Naidoo
    6907 POSTS0 COMMENTS
    Ted Musemwa
    7168 POSTS0 COMMENTS
    Thapelo Manthata
    6864 POSTS0 COMMENTS
    Umr Jansen
    6852 POSTS0 COMMENTS