当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。