PriorityQueue poll() Method in Java
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 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); } } |
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 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); } } |
Initial PriorityQueue: [5, 10, 30, 20, 15] The element at the head of the queue is: 5 Final PriorityQueue: [10, 15, 30, 20]
Recommended Posts:
- LinkedBlockingDeque poll() method in Java
- ConcurrentLinkedDeque poll() method in Java with Example
- LinkedTransferQueue poll() method in Java
- Queue poll() method in Java
- ArrayDeque poll() Method in Java
- ConcurrentLinkedQueue poll() method in Java
- PriorityBlockingQueue poll() method in Java
- ArrayBlockingQueue poll() Method in Java
- LinkedBlockingQueue poll() method in Java
- DelayQueue poll() method in Java with Examples
- PriorityQueue contains() Method in Java
- PriorityQueue add() Method in Java
- PriorityQueue toArray() Method in Java
- PriorityQueue spliterator() method in Java
- PriorityQueue size() Method in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



