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


Java LinkedBlockingDeque element()用法及代碼示例


LinkedBlockingDeque的element()方法返回位於容器前麵的元素。它不會刪除容器中的元素。此方法返回此雙端隊列代表的隊列的頭部。

用法:

public void element()

參數:此方法不接受任何參數。


返回值:此方法返回此雙端隊列代表的隊列的頭部。

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

示例1:

// Java Program Demonstrate element() 
// 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 end of LinkedBlockingDeque 
        LBD.add(10); 
        LBD.add(20); 
        LBD.add(30); 
        LBD.add(40); 
  
        // before removing print queue 
        System.out.println("Linked Blocking Deque: " + LBD); 
  
        System.out.println("Linked Blocking Deque front element: " +  
                                                     LBD.element()); 
    } 
}
輸出:
Linked Blocking Deque: [10, 20, 30, 40]
Linked Blocking Deque front element: 10

示例2:

// Java Program Demonstrate contains() 
// 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<String> LBD 
            = new LinkedBlockingDeque<String>(); 
  
        // Add numbers to end of LinkedBlockingDeque 
        LBD.add("ab"); 
        LBD.add("cd"); 
        LBD.add("fg"); 
        LBD.add("xz"); 
  
        // before removing print queue 
        System.out.println("Linked Blocking Deque: " + LBD); 
  
        System.out.println("Linked Blocking Deque front element: " +  
                                                     LBD.element()); 
    } 
}
輸出:
Linked Blocking Deque: [ab, cd, fg, xz]
Linked Blocking Deque front element: ab

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



相關用法


注:本文由純淨天空篩選整理自gopaldave大神的英文原創作品 LinkedBlockingDeque element() method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。