📌  相关文章
📜  Java中的LinkedBlockingDeque size() 方法

📅  最后修改于: 2022-05-13 01:54:36.851000             🧑  作者: Mango

Java中的LinkedBlockingDeque size() 方法

LinkedBlockingDequesize()方法返回 Deque 容器的当前大小。在调用该函数时,将返回 Deque 容器中的元素数。如果容器容量受限,则它还返回函数调用时容器中存在的元素数量。

句法:

public int size()

返回:此方法返回一个数值,表示容器中元素的数量。

下面的程序说明了 LinkedBlockingDeque 的 size() 方法:

方案一:

// 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 LBD
            = new LinkedBlockingDeque();
  
        // 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

方案二:

// 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 LBD
            = new LinkedBlockingDeque();
  
        // 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: Java/util/concurrent/LinkedBlockingDeque.html#size–