BlockingDeque的size()方法返回Deque容器的当前大小。在调用该函数时,将返回Deque容器中的元素数量。如果容器受容量限制,则它还会返回函数调用时容器中存在的元素数。
用法:
public int size()
返回值:此方法返回一个整数值,该值表示容器中的元素数。
注意:BlockingDeque的size()方法已从Java中的LinkedBlockingDeque类继承。
以下示例程序旨在说明BlockingDeque的size()方法:
示例1:
// Java Program to demonstrate public int size()
// 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 InterruptedException
{
// create object of BlockingDeque
BlockingDeque<Integer> BD
= new LinkedBlockingDeque<Integer>();
// Add numbers to end of BlockingDeque
BD.add(15);
BD.add(20);
BD.add(20);
BD.add(15);
BD.add(15);
BD.add(20);
BD.add(20);
BD.add(15);
// print Deque
System.out.println("Blocking Deque: " + BD);
// prints the Deque after removal
System.out.println("Size of Blocking Deque: "
+ BD.size());
}
}
输出:
Blocking Deque: [15, 20, 20, 15, 15, 20, 20, 15] Size of Blocking Deque: 8
示例2:
// Java Program to demonstrate public int size()
// 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 InterruptedException
{
// create object of BlockingDeque
BlockingDeque<String> BD
= new LinkedBlockingDeque<String>();
// Add numbers to end of BlockingDeque
BD.add("geeks");
BD.add("forGeeks");
BD.add("A Computer");
BD.add("Portal");
// print Dequeue
System.out.println("Blocking Deque: " + BD);
// prints the Deque after removal
System.out.println("Size of Blocking Deque: "
+ BD.size());
}
}
输出:
Blocking Deque: [geeks, forGeeks, A Computer, Portal] Size of Blocking Deque: 4
参考: https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/BlockingDeque.html#size()
相关用法
- Java BlockingDeque add()用法及代码示例
- Java BlockingDeque take()用法及代码示例
- Java BlockingDeque contains()用法及代码示例
- Java BlockingDeque put()用法及代码示例
- Java BlockingDeque addFirst()用法及代码示例
- Java BlockingDeque remove()用法及代码示例
- Java BlockingDeque offerFirst()用法及代码示例
- Java BlockingDeque addLast()用法及代码示例
- Java BlockingDeque putFirst()用法及代码示例
- Java BlockingDeque offerLast()用法及代码示例
- Java BlockingDeque pollLast()用法及代码示例
- Java BlockingDeque pollFirst()用法及代码示例
- Java BlockingDeque poll()用法及代码示例
- Java BlockingDeque peek()用法及代码示例
- Java BlockingDeque removeFirstOccurrence()用法及代码示例
注:本文由纯净天空筛选整理自gopaldave大神的英文原创作品 BlockingDeque size() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。