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


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。