當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。