📌  相关文章
📜  Java中的 ConcurrentLinkedDeque peekFirst() 方法

📅  最后修改于: 2022-05-13 01:55:20.902000             🧑  作者: Mango

Java中的 ConcurrentLinkedDeque peekFirst() 方法

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

句法:

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 cld =
                    new ConcurrentLinkedDeque();
  
        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 cld = 
                           new ConcurrentLinkedDeque();
  
        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: Java/util/concurrent/ConcurrentLinkedDeque.html#peekFirst()