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

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

Java中的 ConcurrentSkipListSet first() 方法

Java .util.concurrent.ConcurrentSkipListSet 的 first() 方法是Java中的一个内置函数,它返回该集合中当前的第一个(最低)元素。

句法:

ConcurrentSkipListSet.first()

返回值:该函数返回此集合中当前的第一个(最低)元素。

异常:如果此集合为空,该函数将抛出 NoSuchElementException。

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

方案一:

// Java Program Demonstrate first()
// method of ConcurrentSkipListSet
  
import java.util.concurrent.ConcurrentSkipListSet;
  
class ConcurrentSkipListSetFirstExample1 {
    public static void main(String[] args)
    {
  
        // Initializing the set
        ConcurrentSkipListSet
            set = new ConcurrentSkipListSet();
  
        // Adding elements to first set
        set.add(10);
        set.add(35);
        set.add(20);
        set.add(25);
  
        System.out.println("The lowest element in the set: "
                                              + set.first());
    }
}
输出:
The lowest element in the set: 10


程序 2:
在 first() 中显示 NoSuchElementException 的程序。

// Java Program Demonstrate first()
// method of ConcurrentSkipListSet
  
import java.util.concurrent.ConcurrentSkipListSet;
  
class ConcurrentSkipListSetFirstExample2 {
    public static void main(String[] args) throws InterruptedException
    {
  
        // Initializing the set
        ConcurrentSkipListSet
            set = new ConcurrentSkipListSet();
        try {
            System.out.println("The lowest element in the set: " +
                                                       set.first());
        }
        catch (Exception e) {
            System.out.println("Exception :" + e);
        }
    }
}
输出:
Exception :java.util.NoSuchElementException

参考: https: Java/util/concurrent/ConcurrentSkipListSet.html#first–