Java LinkedList element() Method
element() The method is in Java's LinkedList is a commonly used method provided by the class; it retrieves but does not remove the first element (head element) of the list. This method is inherited from Queue interface and is one of the basic methods of queue operations.
Method Declaration
public E element()
Return Value
Returns the first element of the list.
exception
If the list is empty (i.e., contains no elements), the method throws NoSuchElementException.
Method details
Basic Functionality
element() The main function of the method isto retrieve without removingthe first element of the list. This is similar to peek() method, but there is a key difference: when the list is empty,peek() returns null, while element() throws an exception.
Comparison with similar methods
| Method Names | Description | Behavior with an empty list |
|---|---|---|
element() |
Retrieve but do not remove the first element | Throws NoSuchElementException |
peek() |
Retrieve but do not remove the first element | Return null |
getFirst() |
Retrieve but do not remove the first element | Throws NoSuchElementException |
remove() |
Retrieve and remove the first element | Throws NoSuchElementException |
poll() |
Retrieve and remove the first element | Return null |
Usage examples
Example 1: Basic usage
Example
import
java.util.LinkedList
;
public
class
ElementExample
{
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"
)
;
// Use the element() method to get the first element
String
firstFruit
=
fruits.
element
(
)
;
System
.
out
.
println
(
"The first fruit is: "
firstFruit
)
;
// List content remains unchanged
System
.
out
.
println
(
"List content: "
fruits
)
;
}
}
Output result
第一个水果是: Apple
链表内容: [Apple, Banana, Cherry]
Example 2: Handling an empty list
Example
import
java.util.LinkedList
;
public
class
EmptyListExample
{
public
static
void
main
(
String
[
]
args
)
{
LinkedList
<
String
emptyList
=
new
LinkedList
<>
(
);
try
{
String
item
=
emptyList.
element
(
)
;
System
.
out
.
println
(
item
)
;
}
catch
(
Exception
e
)
{
System
.
out
.
println
(
"Exception occurred: "
e.
getClass
(
)
.
getSimpleName
(
)
)
;
System
.
out
.
println
(
"Error message: "
e.
getMessage
(
)
)
;
}
}
}
Output result
发生异常: NoSuchElementException
错误信息: null
Best Practices
1. Check whether the list is empty
When using element() Before using the method, it is best to check whether the list is empty to avoid exceptions:
Example
if
(
!
myLinkedList.
isEmpty
(
)
)
{
String
firstElement
=
myLinkedList.
element
(
)
;
// Process the first element
}
else
{
// Handle the empty list case
}
2. Consider using peek() instead
If you are not sure whether the list might be empty and do not want to handle exceptions, consider using peek() method in the component:
Example
String
firstElement
=
myLinkedList.
peek
(
)
;
if
(
firstElement
!=
null
)
{
// Process the first element
}
else
{
// Handle the empty list case
}
3. Performance considerations
element() method has a time complexity of O(1) because it only accesses the head node of the list without any traversal. This is consistent with LinkedList other head-operation methods (such as getFirst(), peek()) and has the same performance characteristics.
Summary
LinkedList 's element() method is a simple but practical tool for safely retrieving the first element of the list. Remember its key difference from peek() method and choose the appropriate method based on your specific needs. When you may encounter an empty list, either check whether the list is empty in advance or consider using peek() method to avoid exception handling.
By using these methods properly, you can more effectively manipulate LinkedList collections and write more robust Java code.
Java LinkedList