What is the ConcurrentLinkedDeque.size() method in Java?

ConcurrentLinkedDeque is a thread-safe, unbounded deque. The null value is not permitted as an element. We can use ConcurrentLinkedDeque when multiple threads share a single deque.

We can use the size method to get the number of elements present in the ConcurrentLinkedDeque object.

Syntax

public int size()

Parameters

This method doesn’t take any parameters.

Return value

The size() method returns an integer value that represents the number of elements present in the deque object.

Code

The code below demonstrates how to use the size method.

import java.util.concurrent.ConcurrentLinkedDeque;
class Size {
public static void main( String args[] ) {
ConcurrentLinkedDeque<String> deque = new ConcurrentLinkedDeque<>();
deque.add("1");
deque.add("2");
deque.add("3");
System.out.println("The deque is " + deque);
System.out.println("The size of the the deque is " + deque.size());
}
}

Explanation

In the code above:

  • Line 1: We import the ConcurrentLinkedDeque class.

  • Line 4: We create a ConcurrentLinkedDeque object with the name deque.

  • Lines 5-7: We use the add() method of the deque object to add three elements ("1","2","3") to the deque.

  • Line 10: We use the size() method of the deque object to get the number of elements present in the deque. In our case, the method returns 3.

Free Resources