當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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–



相關用法


注:本文由純淨天空篩選整理自rupesh_rao大神的英文原創作品 ConcurrentSkipListSet last() method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。