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


Java LinkedBlockingDeque getLast()用法及代码示例


LinkedBlockingDeque的getLast()方法返回Deque容器中的最后一个元素。如果LinkedBlockingDeque为空,则在函数调用时将返回NoSuchElementException。

用法:

public E getLast()

参数:此方法不接受任何参数。


返回值:此方法返回此双端队列容器的最后一个元素。

异常:当双端队列为空时,该函数仅引发一个异常,即NoSuchElementException

以下示例程序旨在说明LinkedBlockingDeque的getLast()方法:

示例1:

// Java Program Demonstrate getLast() 
// method of LinkedBlockingDeque 
  
import java.util.concurrent.LinkedBlockingDeque; 
import java.util.*; 
  
public class GFG { 
    public static void main(String[] args) 
        throws IllegalStateException 
    { 
  
        // create object of LinkedBlockingDeque 
        LinkedBlockingDeque<Integer> LBD 
            = new LinkedBlockingDeque<Integer>(); 
  
        // Add numbers to front of LinkedBlockingDeque 
        LBD.addFirst(7855642); 
        LBD.addFirst(35658786); 
        LBD.addFirst(5278367); 
        LBD.addFirst(74381793); 
  
        // before removing print queue 
        System.out.println("Linked Blocking Deque: " + LBD); 
  
        // prints the last element 
        System.out.println("Linked Blocking Deque first element: " + 
                                                     LBD.getLast()); 
    } 
}
输出:
Linked Blocking Deque: [74381793, 5278367, 35658786, 7855642]
Linked Blocking Deque first element: 7855642

示例2:

// Java Program Demonstrate getLast() 
// method of LinkedBlockingDeque 
// shows exception 
  
import java.util.concurrent.LinkedBlockingDeque; 
import java.util.*; 
  
public class GFG { 
    public static void main(String[] args) 
        throws IllegalStateException 
    { 
  
        // create object of LinkedBlockingDeque 
        LinkedBlockingDeque<Integer> LBD 
            = new LinkedBlockingDeque<Integer>(); 
  
        // Add numbers to front of LinkedBlockingDeque 
        LBD.addFirst(7855642); 
        LBD.addFirst(35658786); 
        LBD.addFirst(5278367); 
        LBD.addFirst(74381793) 
  
            LBD.clear(); 
  
        // Since the container is empty it will throw exception 
        System.out.println("Linked Blocking Deque first element: " +  
                                                     LBD.getLast()); 
    } 
}

输出:

Exception in thread "main" java.util.NoSuchElementException
    at java.util.concurrent.LinkedBlockingDeque.getLast(LinkedBlockingDeque.java:553)
    at GFG.main(GFG.java:28)

参考: https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/LinkedBlockingDeque.html#getLast()



相关用法


注:本文由纯净天空筛选整理自gopaldave大神的英文原创作品 LinkedBlockingDeque getLast() method in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。