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


Java ConcurrentSkipListSet first()用法及代碼示例


java.util.concurrent.ConcurrentSkipListSet的first()方法是Java中的內置函數,它返回當前該集合中的第一個(最低)元素。

用法:

ConcurrentSkipListSet.first()

返回值:該函數返回此集合中當前的第一個(最低)元素。


異常:如果此集合為空,則該函數將引發NoSuchElementException。

以下示例程序旨在說明ConcurrentSkipListSet.first()方法:

示例1:

// Java Program Demonstrate first() 
// method of ConcurrentSkipListSet 
  
import java.util.concurrent.ConcurrentSkipListSet; 
  
class ConcurrentSkipListSetFirstExample1 { 
    public static void main(String[] args) 
    { 
  
        // Initializing the set 
        ConcurrentSkipListSet<Integer> 
            set = new ConcurrentSkipListSet<Integer>(); 
  
        // Adding elements to first set 
        set.add(10); 
        set.add(35); 
        set.add(20); 
        set.add(25); 
  
        System.out.println("The lowest element in the set: "
                                              + set.first()); 
    } 
}
輸出:
The lowest element in the set: 10


示例2:
程序以在first()中顯示NoSuchElementException。

// Java Program Demonstrate first() 
// method of ConcurrentSkipListSet 
  
import java.util.concurrent.ConcurrentSkipListSet; 
  
class ConcurrentSkipListSetFirstExample2 { 
    public static void main(String[] args) throws InterruptedException 
    { 
  
        // Initializing the set 
        ConcurrentSkipListSet<Integer> 
            set = new ConcurrentSkipListSet<Integer>(); 
        try { 
            System.out.println("The lowest element in the set: " + 
                                                       set.first()); 
        } 
        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#first–



相關用法


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