Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Java LinkedList poll() Method

Java LinkedList poll() Method

Java LinkedList Java LinkedList


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:


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:

  1. Queue processing: When using the LinkedList When used as a queue,poll() is the standard dequeue operation.
  2. Task scheduling: Process a task list, taking out the first task to execute each time.
  3. Breadth-first search: In graph or tree traversal algorithms, used to take the next node to be processed from the queue.
  4. Message processing: In a message queue, take out and process the next message.

Notes

  1. Empty list handling: Together with remove() method,poll() When the list is empty, it does not throw an exception but returns null. This makes it more suitable for use when it is uncertain whether the list is empty.
  2. Generic type: Ensure the return value type is handled correctly to avoid ClassCastException.
  3. Concurrent Environment:LinkedList is 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 Java LinkedList