BlockingDeque的element()方法返回位于容器前面的元素。它不会删除容器中的元素。此方法返回此双端队列代表的队列的头部。
用法:
public void element()
参数:此方法不接受任何参数。
返回值:此方法返回此双端队列代表的队列的头部。
注意:BlockingDeque的element()方法已从Java中的LinkedBlockingDeque类继承。
以下示例程序旨在说明BlockingDeque的element()方法:
示例1:
// Java Program Demonstrate element()
// method of BlockingDeque
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.BlockingDeque;
import java.util.*;
public class GFG {
public static void main(String[] args)
throws IllegalStateException
{
// create object of BlockingDeque
BlockingDeque<Integer> BD
= new LinkedBlockingDeque<Integer>();
// Add numbers to end of BlockingDeque
BD.add(10);
BD.add(20);
BD.add(30);
BD.add(40);
// before removing print Deque
System.out.println("Blocking Deque: " + BD);
System.out.println("Blocking Deque front element: " + BD.element());
}
}
输出:
Blocking Deque: [10, 20, 30, 40] Blocking Deque front element: 10
示例2:
// Java Program Demonstrate contains()
// method of BlockingDeque
import java.util.concurrent.LinkedBlockingDeque;
import java.util.*;
import java.util.concurrent.BlockingDeque;
public class GFG {
public static void main(String[] args)
throws IllegalStateException
{
// create object of BlockingDeque
BlockingDeque<String> BD
= new LinkedBlockingDeque<String>();
// Add numbers to end of BlockingDeque
BD.add("ab");
BD.add("cd");
BD.add("fg");
BD.add("xz");
// before removing print Deque
System.out.println("Blocking Deque: " + BD);
System.out.println("Blocking Deque front element: " + BD.element());
}
}
输出:
Blocking Deque: [ab, cd, fg, xz] Blocking Deque front element: ab
参考: https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/BlockingDeque.html#element()
相关用法
- Java BlockingDeque add()用法及代码示例
- Java BlockingDeque contains()用法及代码示例
- Java BlockingDeque take()用法及代码示例
- Java BlockingDeque put()用法及代码示例
- Java BlockingDeque addLast()用法及代码示例
- Java BlockingDeque push()用法及代码示例
- Java BlockingDeque addFirst()用法及代码示例
- Java BlockingDeque takeFirst()用法及代码示例
- Java BlockingDeque putFirst()用法及代码示例
- Java BlockingDeque takeLast()用法及代码示例
- Java BlockingDeque pollLast()用法及代码示例
- Java BlockingDeque pollFirst()用法及代码示例
- Java BlockingDeque removeLastOccurrence()用法及代码示例
- Java BlockingDeque remove()用法及代码示例
- Java BlockingDeque removeFirstOccurrence()用法及代码示例
注:本文由纯净天空筛选整理自gopaldave大神的英文原创作品 BlockingDeque element() method in java with examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。