LinkedBlockingDeque的size()方法返回Deque容器的當前大小。在調用該函數時,將返回Deque容器中的元素數量。如果容器受容量限製,則它還會返回函數調用時容器中存在的元素數。
用法:
public int size()
返回值:此方法返回一個整數值,該值表示容器中的元素數。
以下示例程序旨在說明LinkedBlockingDeque的size()方法:
示例1:
// Java Program to demonstrate public int size()
// method of LinkedBlockingDeque
import java.util.concurrent.LinkedBlockingDeque;
import java.util.*;
public class GFG {
public static void main(String[] args)
throws InterruptedException
{
// create object of LinkedBlockingDeque
LinkedBlockingDeque<Integer> LBD
= new LinkedBlockingDeque<Integer>();
// Add numbers to end of LinkedBlockingDeque
LBD.add(15);
LBD.add(20);
LBD.add(20);
LBD.add(15);
LBD.add(15);
LBD.add(20);
LBD.add(20);
LBD.add(15);
// print Dequeue
System.out.println("Linked Blocking Deque: " + LBD);
// prints the Deque after removal
System.out.println("Size of Linked Blocking Deque: "
+ LBD.size());
}
}
輸出:
Linked Blocking Deque: [15, 20, 20, 15, 15, 20, 20, 15] Size of Linked Blocking Deque: 8
示例2:
// Java Program to demonstrate public int size()
// method of LinkedBlockingDeque
import java.util.concurrent.LinkedBlockingDeque;
import java.util.*;
public class GFG {
public static void main(String[] args)
throws InterruptedException
{
// create object of LinkedBlockingDeque
LinkedBlockingDeque<String> LBD
= new LinkedBlockingDeque<String>();
// Add numbers to end of LinkedBlockingDeque
LBD.add("geeks");
LBD.add("forGeeks");
LBD.add("A Computer");
LBD.add("Portal");
// print Dequeue
System.out.println("Linked Blocking Deque: " + LBD);
// prints the Deque after removal
System.out.println("Size of Linked Blocking Deque: "
+ LBD.size());
}
}
輸出:
Linked Blocking Deque: [geeks, forGeeks, A Computer, Portal] Size of Linked Blocking Deque: 4
參考: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/LinkedBlockingDeque.html#size–
相關用法
- Java LinkedBlockingDeque add()用法及代碼示例
- Java LinkedBlockingDeque put()用法及代碼示例
- Java LinkedBlockingDeque pop()用法及代碼示例
- Java LinkedBlockingDeque take()用法及代碼示例
- Java LinkedBlockingDeque contains()用法及代碼示例
- Java LinkedBlockingDeque peekFirst()用法及代碼示例
- Java LinkedBlockingDeque removeFirstOccurrence()用法及代碼示例
- Java LinkedBlockingDeque remove()用法及代碼示例
- Java LinkedBlockingDeque descendingIterator()用法及代碼示例
- Java LinkedBlockingDeque equals()用法及代碼示例
- Java LinkedBlockingDeque remainingCapacity()用法及代碼示例
- Java LinkedBlockingDeque putLast()用法及代碼示例
- Java LinkedBlockingDeque element()用法及代碼示例
- Java LinkedBlockingDeque removeFirst()用法及代碼示例
- Java LinkedBlockingDeque removeLast()用法及代碼示例
注:本文由純淨天空篩選整理自gopaldave大神的英文原創作品 LinkedBlockingDeque size() method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。