Java PriorityQueue - size() Method
The java.util.PriorityQueue.size() method returns the total number of elements in the priority queue.
Syntax
public int size()
Parameters
No parameter is required.
Return Value
Returns the number of elements in the priority queue.
Exception
NA
Example:
In the example below, the java.util.PriorityQueue.size() method is used to find out the number of elements in the priority queue.
import java.util.*; public class MyClass { public static void main(String[] args) { //creating a priority queue PriorityQueue<Integer> PQueue = new PriorityQueue<Integer>(); //populating the priority queue PQueue.add(10); PQueue.add(20); PQueue.add(30); PQueue.add(40); PQueue.add(50); System.out.println("Size of PQueue: "+ PQueue.size()); //adding one more element in the priority queue PQueue.add(40); System.out.println("Now, Size of PQueue: "+ PQueue.size()); } }
The output of the above code will be:
Size of PQueue: 5 Now, Size of PQueue: 6
❮ Java.util - PriorityQueue