Java中的ConcurrentSkipListSet.first()方法
java.util.concurrent.ConcurrentSkipListSet的first()方法是Java中的一个内置函数,它返回集合中当前第一个(最低的)元素。
语法:
ConcurrentSkipListSet.first()
返回值: 该函数返回集合中当前第一个(最低的)元素。
异常: 如果该集合为空,则该函数会抛出NoSuchElementException异常。
下面的程序说明了ConcurrentSkipListSet.first()方法:
程序1:
// Java程序演示ConcurrentSkipListSet的first()方法
import java.util.concurrent.ConcurrentSkipListSet;
class ConcurrentSkipListSetFirstExample1 {
public static void main(String[] args)
{
// 初始化集合
ConcurrentSkipListSet<Integer>
set = new ConcurrentSkipListSet<Integer>();
// 向集合中添加元素
set.add(10);
set.add(35);
set.add(20);
set.add(25);
System.out.println("集合中最低的元素: "
+ set.first());
}
}
集合中最低的元素: 10
**程序2: ** 演示了first()方法中的NoSuchElementException异常。
// Java程序演示ConcurrentSkipListSet的first()方法
import java.util.concurrent.ConcurrentSkipListSet;
class ConcurrentSkipListSetFirstExample2 {
public static void main(String[] args) throws InterruptedException
{
// 初始化集合
ConcurrentSkipListSet<Integer>
set = new ConcurrentSkipListSet<Integer>();
try {
System.out.println("集合中最低的元素: " +
set.first());
}
catch (Exception e) {
System.out.println("异常:" + e);
}
}
}
异常:java.util.NoSuchElementException
参考: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentSkipListSet.html#first–
极客教程