Java LinkedList peekFirst() Method
peekFirst() The method is in Java's LinkedList is a practical method provided by the class for retrieving but not removing the first element of the list. If the list is empty, it returns null.
Method Syntax
public E peekFirst()
Method Characteristics
- Non-destructive operation: Together with
pollFirst()orremoveFirst()in Vue 2,peekFirst()Does not change the content of the list - Fast access: The time complexity is O(1) because LinkedList directly maintains a reference to the head node
- safe access: Returns when the list is empty
nullinstead of throwing an exception
Use Cases
peekFirst() The method is particularly suitable for the following situations:
- When you need to check the head element of the queue without removing it
- View the next element to be processed before consuming the queue
- When implementing monitoring or logging features
Code Example
Basic Usage Example
Example
import
java.util.LinkedList
;
public
class
PeekFirstExample
{
public
static
void
main
(
String
[
]
args
)
{
LinkedList
<
String
fruits
=
new
LinkedList
<>
(
);
fruits.
add
(
"Apple"
)
;
fruits.
add
(
"Banana"
)
;
fruits.
add
(
"Cherry"
)
;
// View but do not remove the first element
String
firstFruit
=
fruits.
peekFirst
(
)
;
System
.
out
.
println
(
"First fruit: "
firstFruit
)
;
// Output: First fruit: Apple
System
.
out
.
println
(
"LinkedList after peek: "
fruits
)
;
// Output: [Apple, Banana, Cherry]
}
}
Empty list handling example
Example
import
java.util.LinkedList
;
public
class
EmptyListExample
{
public
static
void
main
(
String
[
]
args
)
{
LinkedList
<
Integer
numbers
=
new
LinkedList
<>
(
);
// Safely handle an empty list
Integer
firstNumber
=
numbers.
peekFirst
(
)
;
if
(
firstNumber
==
null
)
{
System
.
out
.
println
(
"The list is empty"
)
;
}
else
{
System
.
out
.
println
(
"First number: "
firstNumber
)
;
}
}
}
Comparison with other similar methods
| Method Names | Function | Behavior when the list is empty | Whether the list is modified |
|---|---|---|---|
peekFirst() |
View the first element | Return null |
No |
getFirst() |
get the first element | Throws NoSuchElementException |
No |
pollFirst() |
Remove and return the first element | Return null |
Is |
removeFirst() |
Remove and return the first element | Throws NoSuchElementException |
Is |
Best Practices
- When it is uncertain whether the list is empty, prefer to use
peekFirst()instead ofgetFirst() - If you need to view and remove an element at the same time, consider using
pollFirst()Improve efficiency - Using in a concurrent environment
peekFirst(), additional synchronization measures are needed
Frequently Asked Questions
Q1: peekFirst() and peek() What is the difference?
peek() and peekFirst() has exactly the same functionality in LinkedList,peek() and is provided to maintain consistency with the Queue interface.
Q2: Why does peekFirst() Return null instead of throwing an exception?
This design allows more flexible handling of empty list situations, is especially suitable for queue operation scenarios, and avoids additional exception-handling code.
Q3: Using in a multi-threaded environment peekFirst() is it safe?
LinkedList is not thread-safe. If you need to use it in a multi-threaded environment, you should consider using Collections.synchronizedList() to wrap it or switch to ConcurrentLinkedDeque.
Java LinkedList