Java中的 ConcurrentSkipListSet last() 方法
Java .util.concurrent.ConcurrentSkipListSet的last()方法是Java中的一个内置函数,它返回该集合中当前的最后一个(最高)元素。
句法:
public E last()
返回值:该函数返回返回此集合中当前的最后一个(最高)元素。
异常:如果此集合为空,该函数将引发 NoSuchElementException。
下面的程序说明了 ConcurrentSkipListSet.last() 方法:
方案一:
// Java program to demonstrate last()
// method of ConcurrentSkipListSet
import java.util.concurrent.ConcurrentSkipListSet;
class ConcurrentSkipListSetLastExample1 {
public static void main(String[] args)
{
// Initializing the set
ConcurrentSkipListSet
set = new ConcurrentSkipListSet();
// Adding elements to this set
set.add(78);
set.add(64);
set.add(12);
set.add(45);
set.add(8);
// Printing the highest element of the set
System.out.println("The highest element of the set: "
+ set.last());
}
}
输出:
The highest element of the set: 78
程序 2:在 last() 中显示 NoSuchElementException 的程序。
// Java program to demonstrate last()
// method of ConcurrentSkipListSet
import java.util.concurrent.ConcurrentSkipListSet;
class ConcurrentSkipListSetLastExample1 {
public static void main(String[] args)
{
// Initializing the set
ConcurrentSkipListSet
set = new ConcurrentSkipListSet();
try {
// Printing the highest element of the set
System.out.println("The highest element of the set: "
+ set.last());
}
catch (Exception e) {
System.out.println("Exception: " + e);
}
}
}
输出:
Exception: java.util.NoSuchElementException
参考: https: Java/util/concurrent/ConcurrentSkipListSet.html#last–