Home »
Java programming language
Java PriorityQueue size() Method with Example
PriorityQueue Class size() method: Here, we are going to learn about the size() method of PriorityQueue Class with its syntax and example.
Submitted by Preeti Jain, on March 11, 2020
PriorityQueue Class size() method
- size() method is available in java.util package.
- size() method is used to return the size (i.e. the number of element exists) of this PriorityQueue.
- size() method is a non-static method, it is accessible with class object only and if we try to access the method with class name then we will get an error.
- size() method does not throw an exception at the time of returning size.
Syntax:
public int size();
Parameter(s):
- It does not accept any parameter.
Return value:
The return type of the method is int, it gets this PriorityQueue size.
Example:
// Java program to demonstrate the example
// of int size() method of
// PriorityQueue
import java.util.*;
public class SizeOfPriorityQueue {
public static void main(String args[]) {
// Instantiate PriorityQueue
PriorityQueue < String > pq = new PriorityQueue < String > ();
// By using add() method is add
// the given element into priority
// queue
pq.add("C");
pq.add("C++");
pq.add("JAVA");
pq.add("PHP");
pq.add("ANDROID");
// Display PriorityQueue
System.out.println("PriorityQueue: " + pq);
// By using size() method isto
// return the size of this
// PriorityQueue
int size = pq.size();
// Display size
System.out.println("pq.size(): " + size);
}
}
Output
PriorityQueue: [ANDROID, C, JAVA, PHP, C++]
pq.size(): 5