📌  相关文章
📜  Java中的 ConcurrentSkipListSet lower() 方法及示例

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

Java中的 ConcurrentSkipListSet lower() 方法及示例

ConcurrentSkipListSet 的lower()方法用于返回此集合中存在的最大元素,该元素严格小于指定元素。如果集合中不存在这样的元素,则此函数将返回 null。

句法:

public E lower (E e)

参数:该方法只接受一个参数,要匹配的指定值。

返回值:此方法返回一个值,该值是集合中存在的最大元素,严格小于指定元素。如果不存在这样的元素,那么它将返回 Null。

异常:此方法引发以下异常:

  • ClassCastException :如果此集合的元素的类与指定的集合不兼容,则抛出此异常。
  • NullPointerException :如果指定的集合为空,则抛出此异常。

下面的程序说明了 ConcurrentSkipListSet 类的 lower()函数:

程序1:在下面的程序中,指定的元素是67,我们的集合中也包含元素67,但是由于lower()方法返回的值严格来说较少,所以没有返回。

// Java program to demonstrate
// ConcurrentSkipListSet lower() method.
  
import java.util.concurrent.ConcurrentSkipListSet;
  
class GfgCSLS {
    public static void main(String[] args)
    {
  
        // Initializing the set
        // using ConcurrentSkipListSet()
        ConcurrentSkipListSet
            set = new ConcurrentSkipListSet();
  
        // Adding elements to this set
        // using add() method
        set.add(17);
        set.add(24);
        set.add(35);
        set.add(67);
        set.add(98);
  
        // Printing the ConcurrentSkipListSet
        System.out.println("ConcurrentSkipListSet: "
                           + set);
  
        // Invoking lower() method
        // to find the largest element
        // less than the specified element
        System.out.println("Largest element: "
                           + set.lower(67));
    }
}

输出:

ConcurrentSkipListSet: [17, 24, 35, 67, 98]
Largest element: 35

程序 2:显示 NullpointerException

// Java program to demonstrate
// ConcurrentSkipListSet lower() method.
  
import java.util.concurrent.ConcurrentSkipListSet;
  
class GfgCSLS {
    public static void main(String[] args)
    {
  
        // Initializing the set
        // using ConcurrentSkipListSet()
        ConcurrentSkipListSet
            set = new ConcurrentSkipListSet();
  
        // Adding elements to this set
        // using add() method
        set.add(17);
        set.add(24);
        set.add(35);
        set.add(67);
        set.add(98);
  
        // Printing the ConcurrentSkipListSet
        System.out.println("ConcurrentSkipListSet: "
                           + set);
  
        try {
            // Invoking lower() method
            // to find the largest element
            // less than the specified element
            System.out.println("Largest element: "
                               + set.lower(null));
        }
        catch (Exception e) {
            System.out.println(e);
        }
    }
}

输出:

ConcurrentSkipListSet: [17, 24, 35, 67, 98]
java.lang.NullPointerException