PriorityBlockingQueue size() method in Java
The size() method of PriorityBlockingQueue is used to find the present size of the queue. It returns the number of elements in the collection. If the collection contains more than Integer.MAX_VALUE elements, then this method returns Integer.MAX_VALUE.
public int size()
Return Value: This method returns the number of elements present in this PriorityBlockingQueue
Below are the program to illustrate size() method of PriorityBlockingQueue:
Queue: [1, 2, 3, 4] Queue Size: 4
Program 2: To demonstrate size() for dynamically changing queue.
Queue: [1, 2, 3, 4] Queue Size: 4 2 removed: true Queue: [1, 4, 3] Queue Size: 3 5 added Queue: [1, 4, 3, 5] Queue Size: 4
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.