Java LinkedList poll() Method
poll() The method is in Java's LinkedList is a very useful method provided by the class; it retrieves and removes the first element (head element) of the list. This method belongs to Queue part of the interface,LinkedList implements this interface, so it can be used as a queue.
Method Syntax:
public E poll()
Return Value:
- If the list is not empty, it returns the first element of the list
- If the list is empty, it returns
null
Method Characteristics
Comparison with Similar Methods
In Java's LinkedList in , there are several similar methods that can be used to get and remove the head element:
| Method | Behavior description | Behavior with an empty list |
|---|---|---|
| poll() | Retrieve and remove the head element | Returns null |
| remove() | Retrieve and remove the head element | Throws NoSuchElementException |
| pop() | Retrieve and remove the head element (stack operation) | Throws NoSuchElementException |
| pollFirst() | Retrieve and remove the first element (same as poll()) | Returns null |
Time Complexity
poll() method has a time complexity of O(1) because it only removes and returns the first element of the list without traversing the entire list.
Usage examples
Basic Usage
Example
import
java.util.LinkedList
;
public
class
PollExample
{
public
static
void
main
(
String
[
]
args
)
{
// Create a LinkedList
LinkedList
<
String
fruits
=
new
LinkedList
<>
(
);
// Add elements
fruits.
add
(
"Apple"
)
;
fruits.
add
(
"Banana"
)
;
fruits.
add
(
"Cherry"
)
;
System
.
out
.
println
(
"Original list: "
fruits
)
;
// Output: [Apple, Banana, Cherry]
// Use the poll() method
String
firstFruit
=
fruits.
poll
(
)
;
System
.
out
.
println
(
"Removed element: "
firstFruit
)
;
// Output: Apple
System
.
out
.
println
(
"Updated list: "
fruits
)
;
// Output: [Banana, Cherry]
}
}
Handling an empty list
Example
import
java.util.LinkedList
;
public
class
EmptyListExample
{
public
static
void
main
(
String
[
]
args
)
{
LinkedList
<
String
emptyList
=
new
LinkedList
<>
(
);
// Use poll() on an empty list
String
result
=
emptyList.
poll
(
)
;
System
.
out
.
println
(
"Result is: "
result
)
;
// Output: null
}
}
Using in queue operations
Example
import
java.util.LinkedList
;
import
java.util.Queue
;
public
class
QueueExample
{
public
static
void
main
(
String
[
]
args
)
{
// Use LinkedList as a Queue
Queue
<
Integer
queue
=
new
LinkedList
<>
(
);
// Enqueue operation
queue.
offer
(
10
)
;
queue.
offer
(
20
)
;
queue.
offer
(
30
)
;
System
.
out
.
println
(
"Queue content: "
queue
)
;
// Output: [10, 20, 30]
// Dequeue operation
while
(
!
queue.
isEmpty
(
)
)
{
int
num
=
queue.
poll
(
)
;
System
.
out
.
println
(
"Processing: "
num
)
;
}
System
.
out
.
println
(
"Final queue state: "
queue
)
;
// Output: []
}
}
Practical Application Scenarios
poll() The method is especially useful in the following scenarios:
- Queue processing: When using the
LinkedListWhen used as a queue,poll()is the standard dequeue operation. - Task scheduling: Process a task list, taking out the first task to execute each time.
- Breadth-first search: In graph or tree traversal algorithms, used to take the next node to be processed from the queue.
- Message processing: In a message queue, take out and process the next message.
Notes
- Empty list handling: Together with
remove()method,poll()When the list is empty, it does not throw an exception but returnsnull. This makes it more suitable for use when it is uncertain whether the list is empty. - Generic type: Ensure the return value type is handled correctly to avoid
ClassCastException. - Concurrent Environment:
LinkedListis not thread-safe. If used in a multi-threaded environment, synchronization issues need to be considered.
By mastering poll() method, you can use LinkedList more effectively as a queue and write cleaner, more robust Java code.
Java LinkedList