ConcurrentLinkedDeque size() method in Java

The size() method of ConcurrentLinkedDeque class in Java is used to find the number of elements present in the ConcurrentLinkedDeque container. In other words, this method tells the current capacity of the container. The value returned by this method is of integral type and in case if the container container more elements than the maximum value of an integer then this method returns the max value of integer i.e., Integer.MAX_VALUE.

Syntax:

ConcurrentLinkedDeque.size()

Parameters: This method doesn’t accepts any parameter.



Return Value: This method returns an integral value which is the current size of the ConcurrentLinkedDeque container.

Below program illustrates size() method of ConcurrentLinkedDeque:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program to demonstrate the
// size of ConcurrentLinkedDeque
  
import java.util.concurrent.*;
  
class ConcurrentLinkedDequeDemo {
    public static void main(String[] args)
    {
        // Create a ConcurrentLinkedDeque
        // using ConcurrentLinkedDeque() contructor
        ConcurrentLinkedDeque<Integer>
            cld = new ConcurrentLinkedDeque<Integer>();
  
        // Adding elements to the collection
        cld.addFirst(12);
        cld.addFirst(70);
        cld.addFirst(1009);
        cld.addFirst(475);
  
        // Displaying the ConcurrentLinkedDeque
        System.out.println("ConcurrentLinkedDeque: "
                           + cld);
  
        // Calculate size
        int size = cld.size();
  
        System.out.println("Size of the collection is: "
                           + size);
    }
}

chevron_right


Output:

ConcurrentLinkedDeque: [475, 1009, 70, 12]
Size of the collection is: 4

Note: Unlike for other collections in Java, this method does not perform the size calculation operation for ConcurrentLinkedDeque in constant time because of the asynchronous nature of these deques and it is possible for the size to change during execution of this method, in which case the returned result will be inaccurate. This method is typically not very useful in concurrent applications.

Reference: https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ConcurrentLinkedDeque.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.