PriorityQueue size() Method in Java
Last Updated :
10 Dec, 2018
The Java.util.PriorityQueue.size() method is used to get the size of the PriorityQueue or the number of elements present in the PriorityQueue.
Syntax:
Priority_Queue.size()
Parameters: This method does not takes any parameter.
Return Value: The method returns the size or the number of elements present in the PriorityQueue.
Below programs illustrate the Java.util.PriorityQueue.size() method:
Program 1:
import java.util.*;
public class PriorityQueueDemo {
public static void main(String args[])
{
PriorityQueue<String> queue = new PriorityQueue<String>();
queue.add( "Welcome" );
queue.add( "To" );
queue.add( "Geeks" );
queue.add( "For" );
queue.add( "Geeks" );
System.out.println( "PriorityQueue: " + queue);
System.out.println( "The size of the queue is: " + queue.size());
}
}
|
Output:
PriorityQueue: [For, Geeks, To, Welcome, Geeks]
The size of the queue is: 5
Program 2:
import java.util.*;
public class PriorityQueueDemo {
public static void main(String args[])
{
PriorityQueue<Integer> queue = new PriorityQueue<Integer>();
queue.add( 10 );
queue.add( 15 );
queue.add( 30 );
queue.add( 20 );
queue.add( 5 );
queue.add( 18 );
System.out.println( "PriorityQueue: " + queue);
System.out.println( "The size of the queue is: " + queue.size());
}
}
|
Output:
PriorityQueue: [5, 10, 18, 20, 15, 30]
The size of the queue is: 6
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...