📜  Java中的 ConcurrentSkipListSet ceiling() 方法

📅  最后修改于: 2022-05-13 01:55:48.712000             🧑  作者: Mango

Java中的 ConcurrentSkipListSet ceiling() 方法

Java .util.concurrent.ConcurrentSkipListSet.ceiling()方法是Java中的一个内置函数,它返回此集合中大于或等于给定元素的最小元素,如果没有这样的元素,则返回 null。

句法:

ConcurrentSkipListSet.ceiling(E e)

参数:该函数接受单个参数e ,即要匹配的元素。

返回值:该函数返回大于或等于e的最小元素,如果没有这样的元素,则返回 null。

异常:该函数显示以下异常:

  • ClassCastException – 如果指定的元素不能与当前在集合中的元素进行比较
  • NullPointerException – 如果指定元素为 null

    下面的程序说明了 ConcurrentSkipListSet.ceiling() 方法:

    程序 1:找到数字的上限。

    // Java Program Demonstrate ceiling()
    // method of ConcurrentSkipListSet 
      
    import java.util.concurrent.*;
      
    class ConcurrentSkipListSetCeilingExample1 {
        public static void main(String[] args)
        {
            // Creating a set object
            ConcurrentSkipListSet Lset = 
                            new ConcurrentSkipListSet();
      
            // 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));
        }
    }
    
    输出:
    The set contains: 
    13 31 45 72 89 
    Ceiling of 35: 45
    
    Ceiling of 100: null
    

    程序 2:在 ceiling() 中显示 NullPointerException。

    // 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 Lset = 
                                new ConcurrentSkipListSet();
      
            // 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);
            }
        }
    }
    
    输出:
    Exception : java.lang.NullPointerException
    

    参考: https: Java/util/concurrent/ConcurrentSkipListSet.html#ceiling(E)