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–
相關用法
- Java ConcurrentSkipListSet contains()用法及代碼示例
- Java ConcurrentSkipListSet first()用法及代碼示例
- Java ConcurrentSkipListSet add()用法及代碼示例
- Java ConcurrentSkipListSet remove()用法及代碼示例
- Java ConcurrentSkipListSet higher()用法及代碼示例
- Java ConcurrentSkipListSet size()用法及代碼示例
- Java ConcurrentSkipListSet headSet()用法及代碼示例
- Java ConcurrentSkipListSet clear()用法及代碼示例
- Java ConcurrentSkipListSet descendingSet()用法及代碼示例
- Java ConcurrentSkipListSet equals()用法及代碼示例
- Java ConcurrentSkipListSet pollFirst()用法及代碼示例
- Java ConcurrentSkipListSet removeAll()用法及代碼示例
- Java ConcurrentSkipListSet subSet()用法及代碼示例
- Java ConcurrentSkipListSet pollLast()用法及代碼示例
- Java ConcurrentSkipListSet floor()用法及代碼示例
注:本文由純淨天空篩選整理自rupesh_rao大神的英文原創作品 ConcurrentSkipListSet last() method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。