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


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


java.util.concurrent.ConcurrentLinkedDeque.peekFirst()是Java中的内置方法,
检索的第一个元素这个双端队列而不删除它。函数返回空值如果双端队列为空。

用法:

Conn_Linked_Deque.peekFirst() 

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


返回值:该函数返回此双端队列中存在的第一个元素,当双端队列为空时,该函数返回NULL。

以下示例程序旨在说明peekFirst()方法:

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

// Java Program Demonstrate peekFirst() 
// 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(110); 
        cld.addFirst(55); 
        cld.addFirst(76); 
  
        // Displaying the existing LinkedDeque 
        System.out.println("Elements in"
                           + "the LinkedDeque: " + cld); 
  
        // Displaying the first element 
        System.out.println("First Element in"
                   + "the LinkedDeque: " + cld.peekFirst()); 
    } 
}
输出:
Elements inthe LinkedDeque: [76, 55, 110, 12]
First Element inthe LinkedDeque: 76

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

// Java Program Demonstrate peekFirst() 
// 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("Gfg"); 
        cld.addFirst("GeeksforGeeks"); 
        cld.addFirst("Geeks"); 
  
        // Displaying the existing LinkedDeque 
        System.out.println("Elements in"
                           + "the LinkedDeque: " + cld); 
  
        // Displaying the First element 
        System.out.println("First Element in"
                       + "the LinkedDeque: " + cld.peekFirst()); 
    } 
}
输出:
Elements inthe LinkedDeque: [Geeks, GeeksforGeeks, Gfg, GFG]
First Element inthe LinkedDeque: Geeks

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



相关用法


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