ConcurrentLinkedQueue size() method in Java

The size() method of ConcurrentLinkedQueue is used to returns number of elements that this ConcurrentLinkedQueue contains.

Syntax:

public int size()

Returns: This method number of elements in this ConcurrentLinkedQueue.



Below programs illustrate size() method of ConcurrentLinkedQueue:

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program Demonstrate size()
// method of ConcurrentLinkedQueue
  
import java.util.concurrent.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create an ConcurrentLinkedQueue
        ConcurrentLinkedQueue<Integer>
            queue = new ConcurrentLinkedQueue<Integer>();
  
        // 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);
    }
}

chevron_right


Output:

ConcurrentLinkedQueue: [4353, 7824, 78249, 8724]
Size of ConcurrentLinkedQueue: 4

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program Demonstrate size()
// method of ConcurrentLinkedQueue
  
import java.util.concurrent.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create an ConcurrentLinkedQueue
        ConcurrentLinkedQueue<String>
            queue = new ConcurrentLinkedQueue<String>();
  
        // 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);
    }
}

chevron_right


Output:

ConcurrentLinkedQueue: [Aman, Amar, Sanjeet, Rabi]
Size of ConcurrentLinkedQueue: 4

After 2 removal of elements

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

Reference: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentLinkedQueue.html#size–



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.