Skip to content

Commit 989fcb7

Browse files
authored
Added md file for poll term (#7477)
* Added md file for poll term * content fixes * Update poll.md * Tag order fix * comment fix * format fixes ---------
1 parent 9b1798b commit 989fcb7

File tree

1 file changed

+66
-0
lines changed
  • content/java/concepts/queue/terms/poll

1 file changed

+66
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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+
```

0 commit comments

Comments
 (0)