Java中的 BlockingDeque size() 方法及示例
BlockingDeque的size()方法返回 Deque 容器的当前大小。在调用该函数时,将返回 Deque 容器中的元素数。如果容器容量受限,则它还返回函数调用时容器中存在的元素数量。
句法:
public int size()
返回:此方法返回一个整数值,表示容器中元素的数量。
注意: BlockingDeque的size()方法继承自Java中的 LinkedBlockingDeque 类。
下面的程序说明了 BlockingDeque 的 size() 方法:
方案一:
// 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 BD
= new LinkedBlockingDeque();
// 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
方案二:
// 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 BD
= new LinkedBlockingDeque();
// 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: Java/util/concurrent/BlockingDeque.html#size()