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


Java ConcurrentLinkedDeque pollFirst()用法及代码示例


java.util.concurrent.ConcurrentLinkedDeque.pollFirst()是Java中的内置方法,它检索此双端队列的第一个元素并将其删除。如果双端队列为空,则该方法返回NULL。

用法:

Conn_Linked_Deque.pollFirst()

参数:该函数不接受任何参数。


返回值:该函数返回此双端队列的第一个元素。如果此双端队列为空,则该函数返回NULL。

以下示例程序旨在说明pollFirst()方法的使用:

示例1:此程序涉及带有Integer元素的双端队列。

// Java Program Demonstrate pollFirst()  
// method of ConcurrentLinkedDeque    
  
import java.util.concurrent.*; 
  
class ConcurrentLinkedDequeDemo { 
    public static void main(String[] args) 
    { 
        ConcurrentLinkedDeque<Integer> cld =  
                    new ConcurrentLinkedDeque<Integer>(); 
  
        cld.addFirst(12); 
        cld.addFirst(70); 
        cld.addFirst(1009); 
        cld.addFirst(475); 
  
        // Displaying the existing LinkedDeque 
        System.out.println("Elements in"
                           + "the LinkedDeque: " + cld); 
  
        // Display and remove the first element 
        System.out.println("Element removed : "
                           + cld.pollFirst()); 
  
        // Displaying the elements 
        System.out.println("Elements in"
                           + "the LinkedDeque: " + cld); 
    } 
}
输出:
Elements inthe LinkedDeque: [475, 1009, 70, 12]
Element removed : 475
Elements inthe LinkedDeque: [1009, 70, 12]

示例2:该程序涉及带有String元素的双端队列。

// Java Program Demonstrate pollFirst()  
// method of ConcurrentLinkedDeque    
  
import java.util.concurrent.*; 
  
class ConcurrentLinkedDequeDemo { 
    public static void main(String[] args) 
    { 
        ConcurrentLinkedDeque<String> cld =  
                        new ConcurrentLinkedDeque<String>(); 
  
        cld.addFirst("GFG"); 
        cld.addFirst("Geeks"); 
        cld.addFirst("Gfg"); 
        cld.addFirst("Geeks"); 
  
        // Displaying the existing LinkedDeque 
        System.out.println("Elements in"
                           + "the LinkedDeque: " + cld); 
  
        // Display and remove the First element 
        System.out.println("Element removed : "
                           + cld.pollFirst()); 
  
        // Displaying the elements 
        System.out.println("Elements in"
                           + "the LinkedDeque: " + cld); 
    } 
}
输出:
Elements inthe LinkedDeque: [Geeks, Gfg, Geeks, GFG]
Element removed : Geeks
Elements inthe LinkedDeque: [Gfg, Geeks, GFG]

参考: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentLinkedDeque.html#pollFirst()



相关用法


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