Java ConcurrentSkipListSet last()方法
java.util.concurrent.ConcurrentSkipListSet 的 last() 方法是Java中的一个内置函数,用于返回当前该集合中最后一个(最高)元素。
语法
public E last()
返回值: 该函数返回当前在这个集合中的最后(最高)元素。
异常: 如果这个集合是空的,该函数会抛出NoSuchElementException。
下面的程序说明了ConcurrentSkipListSet.last()方法。
程序1 :
// 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<Integer>
set = new ConcurrentSkipListSet<Integer>();
// 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<Integer>
set = new ConcurrentSkipListSet<Integer>();
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://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentSkipListSet.html#last-