This repository was archived by the owner on Feb 27, 2026. It is now read-only.
File tree Expand file tree Collapse file tree
content/java/concepts/queue/terms/poll Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ ---
2+ Title : ' poll()'
3+ Description : ' Retrieves and removes the head of the queue, or returns null if the queue is empty.'
4+ Subjects :
5+ - ' Computer Science'
6+ - ' Data Science'
7+ Tags :
8+ - ' Java'
9+ - ' Queues'
10+ CatalogContent :
11+ - ' learn-java'
12+ - ' paths/computer-science'
13+ ---
14+
15+ In Java, the ** ` poll() ` ** method of a queue retrieves and removes the head element, or returns ` null ` if the queue is empty.
16+
17+ ## Syntax
18+
19+ ``` pseudo
20+ E poll()
21+ ```
22+
23+ ** Parameters:**
24+
25+ The ` poll() ` method does not take any parameters.
26+
27+ ** Return value:**
28+
29+ - Returns the head element (` E ` ) of the queue and removes it.
30+ - Returns ` null ` if the queue is empty.
31+
32+ ## Example
33+
34+ In this example, a queue is used to store elements and the ` poll() ` method processes them one by one in FIFO order:
35+
36+ ``` java
37+ import java.util.Queue ;
38+ import java.util.LinkedList ;
39+
40+ public class PollQueueExample {
41+ public static void main (String [] args ) {
42+ // Create a queue
43+ Queue<String > queue = new LinkedList<> ();
44+
45+ // Add elements to the queue
46+ queue. add(" A" );
47+ queue. add(" B" );
48+ queue. add(" C" );
49+
50+ // Process elements
51+ while (! queue. isEmpty()) {
52+ // Removes and returns the head of the queue
53+ String element = queue. poll();
54+ System . out. println(" Processing element: " + element);
55+ }
56+ }
57+ }
58+ ```
59+
60+ The output of this code is:
61+
62+ ``` shell
63+ Processing element: A
64+ Processing element: B
65+ Processing element: C
66+ ```
You can’t perform that action at this time.
0 commit comments