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


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