Java LinkedList removeFirst() Method
removeFirst() is a method in Java's LinkedList is a method provided by the class for removing and returning the first element of the list. This method belongs to java.util.LinkedList class and is one of the basic operations of the doubly linked list data structure.
Method Syntax
public E removeFirst()
Parameters
This methodNot requireddoes not accept any parameters.
Return Value
- Returns the removed first element of the list
- The return type is the generic type E (the type specified when LinkedList is declared)
Method behavior
Normal case
When the list is not empty:
- The method removes the first element from the list
- Returns the removed element
- The original second element becomes the new first element
- The list's size decreases by 1
Exception cases
When the list is empty (size = 0), calling this method throws NoSuchElementException exception.
Usage examples
Basic example
Example
import
java.util.LinkedList
;
public
class
LinkedListDemo
{
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 linked list: "
fruits
)
;
// Output: [Apple, Banana, Cherry]
// Use the removeFirst() method
String
firstFruit
=
fruits.
removeFirst
(
)
;
System
.
out
.
println
(
"Removed element: "
firstFruit
)
;
// Output: Apple
System
.
out
.
println
(
"List after operation: "
fruits
)
;
// Output: [Banana, Cherry]
}
}
Handle the empty list case
Example
import
java.util.LinkedList
;
import
java.util.NoSuchElementException
;
public
class
EmptyLinkedListDemo
{
public
static
void
main
(
String
[
]
args
)
{
LinkedList
<
Integer
numbers
=
new
LinkedList
<>
(
);
try
{
int
first
=
numbers.
removeFirst
(
)
;
System
.
out
.
println
(
"Removed element: "
first
)
;
}
catch
(
NoSuchElementException
e
)
{
System
.
out
.
println
(
"List is empty, cannot remove element!"
)
;
}
}
}
Comparison with related methods
| Method Names | Description | Behavior when the list is empty |
|---|---|---|
removeFirst() |
Remove and return the first element | Throws NoSuchElementException |
pollFirst() |
Remove and return the first element | Returns null |
pop() |
Remove and return the first element (stack operation) | Throws NoSuchElementException |
remove() |
Remove and return the first element | Throws NoSuchElementException |
Practical Application Scenarios
- Queue processing: When using LinkedList as a queue, removeFirst() can be used to implement FIFO (first-in-first-out) operations
- Undo operation: In some applications, you may need to remove the most recently added operation record
- Task scheduling: Process the first task in a task list
Performance considerations
removeFirst() method has a time complexity of O(1) because:
- LinkedList internally maintains head and tail pointers
- Removing the first element only requires adjusting the head pointer and the references of adjacent nodes
- There is no need to move elements as with an array
Best Practices
- Empty list check: Before using removeFirst(), it is best to check whether the list is empty
- Alternative methods: If you do not want to handle exceptions, you can consider using the pollFirst() method
- Pairs with addFirst(): Often used together with the addFirst() method to implement a stack structure
Example
LinkedList
<
String
stack
=
new
LinkedList
<>
(
);
stack.
addFirst
(
"First"
)
;
// Push onto stack
stack.
addFirst
(
"Second"
)
;
// Push onto stack
String
top
=
stack.
removeFirst
(
)
;
// Pop from stack
Summary
LinkedList.removeFirst() is an efficient method for removing and returning the first element of the list. Understanding this method's behavior and exceptions is very important for using LinkedList correctly. In actual development, depending on whether you need to handle the empty list case, you can choose to use removeFirst() or pollFirst().
Java LinkedList