Java poll()

Anonymous contributor's avatar
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.

  • 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

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 null if 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 queue
Queue<String> queue = new LinkedList<>();
// Add elements to the queue
queue.add("A");
queue.add("B");
queue.add("C");
// Process elements
while (!queue.isEmpty()) {
// Removes and returns the head of the queue
String element = queue.poll();
System.out.println("Processing element: " + element);
}
}
}

The output of this code is:

Processing element: A
Processing element: B
Processing element: C

All contributors

Contribute to 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