Java ArrayBlockingQueue size() Method

The size() method of Java ArrayBlockingQueue class returns the total number of elements in the ArrayBlockingQueue.

Syntax:

Parameters:

NA

Specified By:

The size() method of ArrayBlockingQueue class is specified by:

  • Size in interface Collection<E>.
  • Size in class AbstractCollection<E>

Return Value:

The size() method returns the total number of elements in this queue.

Example 1

Output:

Total elements in the queue = 5
Elements are :
67
109
98
123
100

Example 2

Output:

Total Patients = 6
Megha
Reema
Supriya
Anoop
Sonia
Tejashvi

Patients recovered and discharged : [Megha, Reema, Supriya, Anoop]

Patients left in the hospital = 2
Sonia
Tejashvi

Example 3

Output:

Exception in thread "main" java.lang.NullPointerException
	at java.util.concurrent.ArrayBlockingQueue.checkNotNull(ArrayBlockingQueue.java:150)
	at java.util.concurrent.ArrayBlockingQueue.offer(ArrayBlockingQueue.java:325)
	at java.util.AbstractQueue.add(AbstractQueue.java:95)
	at java.util.concurrent.ArrayBlockingQueue.add(ArrayBlockingQueue.java:312)
	at com.javaTpoint.ArrayBlockingQueueSizeExample3.main(ArrayBlockingQueueSizeExample3.java:10)