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

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

Java中的 ConcurrentLinkedQueue size() 方法

ConcurrentLinkedQueuesize()方法用于返回此 ConcurrentLinkedQueue 包含的元素数。

句法:

public int size()

返回:此方法此 ConcurrentLinkedQueue中的元素数

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

示例 1:

// Java Program Demonstrate size()
// method of ConcurrentLinkedQueue
  
import java.util.concurrent.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create an ConcurrentLinkedQueue
        ConcurrentLinkedQueue
            queue = new ConcurrentLinkedQueue();
  
        // Add Numbers to queue
        queue.add(4353);
        queue.add(7824);
        queue.add(78249);
        queue.add(8724);
  
        // Displaying the existing ConcurrentLinkedQueue
        System.out.println("ConcurrentLinkedQueue: " + queue);
  
        // apply size()
        int size = queue.size();
  
        // print after applying size method
        System.out.println("Size of ConcurrentLinkedQueue: " + size);
    }
}
输出:
ConcurrentLinkedQueue: [4353, 7824, 78249, 8724]
Size of ConcurrentLinkedQueue: 4

示例 2:

// Java Program Demonstrate size()
// method of ConcurrentLinkedQueue
  
import java.util.concurrent.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create an ConcurrentLinkedQueue
        ConcurrentLinkedQueue
            queue = new ConcurrentLinkedQueue();
  
        // Add String to queue
        queue.add("Aman");
        queue.add("Amar");
        queue.add("Sanjeet");
        queue.add("Rabi");
  
        // Displaying the existing ConcurrentLinkedQueue
        System.out.println("ConcurrentLinkedQueue: " + queue);
  
        // apply size() on queue
        int size = queue.size();
  
        // print after applying size method
        System.out.println("Size of ConcurrentLinkedQueue: "
                           + size);
  
        // removal of some elements
        queue.poll();
        queue.poll();
  
        // get size of ConcurrentLinkedQueue again
        size = queue.size();
  
        // Displaying the existing ConcurrentLinkedQueue
        System.out.println("After 2 removal of elements\n"
                           + "ConcurrentLinkedQueue: " + queue);
  
        // print after applying size method
        System.out.println("Size of ConcurrentLinkedQueue: "
                           + size);
    }
}
输出:
ConcurrentLinkedQueue: [Aman, Amar, Sanjeet, Rabi]
Size of ConcurrentLinkedQueue: 4

After 2 removal of elements

ConcurrentLinkedQueue: [Sanjeet, Rabi]
Size of ConcurrentLinkedQueue: 2

参考: https: Java/util/concurrent/ConcurrentLinkedQueue.html#size–