Java中的ConcurrentSkipListSet last()方法

这个 最后() 方法 JAVAutil。同时发生的ConcurrentSkipListSet 是Java中的内置函数,返回该集合中当前的最后一个(最高)元素。

null

语法:

public E last()

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

例外情况: 如果此集合为空,该函数将抛出NoTouchElementException。

下面的程序演示了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()中显示NoTouchElementException的程序。

// 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–

© 版权声明
THE END
喜欢就支持一下吧
点赞5 分享