PriorityQueue poll() Method in Java Last Updated : 10 Dec, 2018 Comments Improve Suggest changes Like Article Like Report The java.util.PriorityQueue.poll() method in Java is used to retrieve or fetch and remove the first element of the Queue or the element present at the head of the Queue. The peek() method only retrieved the element at the head but the poll() also removes the element along with the retrieval. It returns NULL if the queue is empty. Syntax: Priority_Queue.poll() Parameters: The method does not take any parameters. Return Value: The method returns the element at the head of the Queue else returns NULL if the Queue is empty. Below programs illustrate the use of java.util.PriorityQueue.poll() method: Program 1: Java // Java code to illustrate poll() 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("Initial PriorityQueue: " + queue); // Fetching and removing the element at the head of the queue System.out.println("The element at the head of the" + " queue is: " + queue.poll()); // Displaying the Queue after the Operation System.out.println("Final PriorityQueue: " + queue); } } Output: Initial PriorityQueue: [For, Geeks, To, Welcome, Geeks] The element at the head of the queue is: For Final PriorityQueue: [Geeks, Geeks, To, Welcome] Program 2: Java // Java code to illustrate poll() 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); // Displaying the PriorityQueue System.out.println("Initial PriorityQueue: " + queue); // Fetching the element at the head of the queue System.out.println("The element at the head of the" + " queue is: " + queue.poll()); // Displaying the Queue after the Operation System.out.println("Final PriorityQueue: " + queue); } } Output: Initial PriorityQueue: [5, 10, 30, 20, 15] The element at the head of the queue is: 5 Final PriorityQueue: [10, 15, 30, 20] Comment More infoAdvertise with us Next Article PriorityQueue poll() 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 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 PriorityQueue offer() Method in Java The java.util.PriorityQueue.offer() method is used to insert a particular element into the Priority Queue. It acts similar to the add() method of Priority Queue. Syntax: Priority_Queue.offer(Object element) Parameters: The parameter element is of the type PriorityQueue and refers to the element to b 2 min read PriorityBlockingQueue poll() method in Java 1. poll() Method The poll() method of PriorityBlockingQueue retrieves and removes element from head of this PriorityBlockingQueue. This method returns the element it removes from PriorityBlockingQueue but when the queue is empty then method will return null. Syntax: public E poll() Returns: This met 4 min read Queue poll() method in Java The poll() method of Queue Interface returns and removes the element at the front end of the container. It deletes the element in the container. The method does not throws an exception when the Queue is empty, it returns null instead. Syntax: E poll() Returns: This method returns the element at the 3 min read PriorityQueue add() Method in Java The Java.util.PriorityQueue.add() method in Java is used to add a specific element into a PriorityQueue. This method internally just calls the Java.util.PriorityQueue.offer() method with the value passed to it. So, it exactly works like offer() method. Syntax: Priority_Queue.add(Object element) Para 2 min read Like