Java BlockingDeque size()方法及示例
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()