PriorityQueue size() Method in Java Last Updated : 10 Dec, 2018 Summarize Comments Improve Suggest changes Share Like Article Like Report 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: Java // Java code to illustrate size() import java.util.*; public class PriorityQueueDemo { public static void main(String args[]) { // Creating an empty PriorityQueue PriorityQueue<String> queue = new PriorityQueue<String>(); // Use add() method to add elements into the Queue queue.add("Welcome"); queue.add("To"); queue.add("Geeks"); queue.add("For"); queue.add("Geeks"); // Displaying the PriorityQueue System.out.println("PriorityQueue: " + queue); // Displaying the size of the PriorityQueue 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: Java // Java code to illustrate size() import java.util.*; public class PriorityQueueDemo { public static void main(String args[]) { // Creating an empty PriorityQueue PriorityQueue<Integer> queue = new PriorityQueue<Integer>(); // Use add() method to add elements into the Queue queue.add(10); queue.add(15); queue.add(30); queue.add(20); queue.add(5); queue.add(18); // Displaying the PriorityQueue System.out.println("PriorityQueue: " + queue); // Displaying the size of the PriorityQueue 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 Comment More infoAdvertise with us Next Article PriorityBlockingQueue size() method in Java C chinmoy lenka Follow Improve Article Tags : Misc Java Java-Collections Java - util package Java-Functions java-priority-queue +2 More Practice Tags : JavaJava-CollectionsMisc Similar Reads 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. Syntax: public int size() Return Value: This met 2 min read PriorityQueue remove() Method in Java The remove() method of PriorityQueue class of java.util package is used to remove a particular element from a PriorityQueue. As we all know that the elements while entering into the priority queue are not sorted but as we all know while taking out elements from the priority queue the elements are al 5 min read PriorityQueue peek() Method in Java The java.util.PriorityQueue.peek() method in Java is used to retrieve or fetch the first element of the Queue or the element present at the head of the Queue. The element retrieved does not get deleted or removed from the Queue. Syntax: Priority_Queue.peek() Parameters: The method does not take any 2 min read PriorityBlockingQueue take() method in Java The take() method of PriorityBlockingQueue returns head of the queue after removing it. If queue is empty, then this method will wait until an element becomes available. Syntax: public E take() throws InterruptedException Returns: This method returns value at the head of this PriorityBlockingQueue. 2 min read PriorityBlockingQueue put() method in Java The put(E e) method of PriorityBlockingQueue is used to add an element into this queue. This method inserts the specified element into this priority queue. Since the queue is unbounded, this method will be never be blocked.Syntax: public void put(E e) Parameter: This method accepts a mandatory param 2 min read PriorityBlockingQueue spliterator() method in Java The spliterator() method of PriorityBlockingQueue returns a Spliterator of the same elements as PriorityBlockingQueue. The returned iterator is weakly consistent. It can be used with Streams in Java 8. Also it can traverse elements individually and in bulk too.Spliterator is better way to traverse o 2 min read Like