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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。