📌  相关文章
📜  Java中的 ConcurrentSkipListSet floor() 方法

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

Java中的 ConcurrentSkipListSet floor() 方法

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

句法:

ConcurrentSkipListSet.floor(E e)

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

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

异常:该函数抛出以下异常:

  • ClassCastException – 如果指定的元素无法与集合中当前的元素进行比较。
  • NullPointerException – 如果指定元素为 null

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

    方案一:

    // Java program to demonstrate floor()
    // method of ConcurrentSkipListSet
      
    import java.util.concurrent.*;
      
    class ConcurrentSkipListSetFloorExample1 {
        public static void main(String[] args)
        {
            // Creating a set object
            ConcurrentSkipListSet
                Lset = new ConcurrentSkipListSet();
      
            // Adding elements to this set
            for (int i = 10; i <= 50; i += 10)
                Lset.add(i);
      
            // Finding floor of 20 in the set
            System.out.println("The floor of 20 in the set "
                               + Lset.floor(20));
      
            // Finding floor of 39 in the set
            System.out.println("The floor of 39 in the set "
                               + Lset.floor(39));
      
            // Finding floor of 9 in the set
            System.out.println("The floor of 10 in the set "
                               + Lset.floor(9));
        }
    }
    
    输出:
    The floor of 20 in the set 20
    The floor of 39 in the set 30
    The floor of 10 in the set null
    

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

    // Java program to demonstrate floor()
    // method of ConcurrentSkipListSet
      
    import java.util.concurrent.*;
      
    class ConcurrentSkipListSetFloorExample2 {
        public static void main(String[] args)
        {
            // Creating a set object
            ConcurrentSkipListSet
                Lset = new ConcurrentSkipListSet();
      
            // Adding elements to this set
            for (int i = 10; i <= 50; i += 10)
                Lset.add(i);
      
            // Trying to find the floor of null
            try {
                System.out.println("The floor of null in the set "
                                   + Lset.floor(null));
            }
            catch (Exception e) {
                System.out.println("Exception: " + e);
            }
        }
    }
    
    输出:
    Exception: java.lang.NullPointerException
    

    参考: https: Java/util/concurrent/ConcurrentSkipListSet.html#floor-E-