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–
相關用法
- Java ConcurrentSkipListSet contains()用法及代碼示例
- Java ConcurrentSkipListSet add()用法及代碼示例
- Java ConcurrentSkipListSet last()用法及代碼示例
- 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 first() method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。