Java poll()
Anonymous contributor
Published Aug 28, 2025
Contribute to Docs
In Java, the poll() method of a queue retrieves and removes the head element, or returns null if the queue is empty.
Syntax
E poll()
Parameters:
The poll() method does not take any parameters.
Return value:
- Returns the head element (
E) of the queue and removes it. - Returns
nullif the queue is empty.
Example
In this example, a queue is used to store elements and the poll() method processes them one by one in FIFO order:
import java.util.Queue;import java.util.LinkedList;public class PollQueueExample {public static void main(String[] args) {// Create a queueQueue<String> queue = new LinkedList<>();// Add elements to the queuequeue.add("A");queue.add("B");queue.add("C");// Process elementswhile (!queue.isEmpty()) {// Removes and returns the head of the queueString element = queue.poll();System.out.println("Processing element: " + element);}}}
The output of this code is:
Processing element: AProcessing element: BProcessing element: C
All contributors
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Java on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours
- Learn to code in Java — a robust programming language used to create software, web and mobile apps, and more.
- Beginner Friendly.17 hours