Java Linkedlist Element
π
2026-06-22 | π Java
Java LinkedList element() Method | Novice Tutorial
[ Java LinkedList](#)
* * *
The `element()` method is a commonly used method provided by the `LinkedList` class in Java, used to retrieve but not remove the first element (head element) of the linked list. This method is inherited from the `Queue` interface and is one of the fundamental methods for queue operations.
### Method Declaration
public E element()
### Return Value
Returns the first element of the linked list.
### Exception
If the linked list is empty (i.e., contains no elements), this method throws NoSuchElementException.
* * *
## Method Details
### Basic Functionality
The main function of the `element()` method is to **retrieve but not remove** the first element of the linked list. This is similar to the `peek()` method, but with one key difference: when the linked list is empty, `peek()` returns `null`, while `element()` throws an exception.
### Comparison with Similar Methods
| Method Name | Function Description | Behavior When List is Empty |
| --- | --- | --- |
| `element()` | Retrieve but not remove the first element | Throws `NoSuchElementException` |
| `peek()` | Retrieve but not remove the first element | Returns `null` |
| `getFirst()` | Retrieve but not remove the first element | Throws `NoSuchElementException` |
| `remove()` | Retrieve and remove the first element | Throws `NoSuchElementException` |
| `poll()` | Retrieve and remove the first element | Returns `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 fruits =new LinkedList();
// Add elements
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Cherry");
// Use element() method to get the first element
String firstFruit = fruits.element();
System.out.println("First fruit is: "+ firstFruit);
// The list content remains unchanged
System.out.println("List content: "+ fruits);
}
}
#### Output
First fruit is: AppleList content: [Apple, Banana, Cherry]
### Example 2: Handling an Empty List
## Example
import java.util.LinkedList;
public class EmptyListExample {
public static void main(String[] args){
LinkedList 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
Exception occurred: NoSuchElementExceptionError message: null
* * *
## Best Practices
### 1. Check if the List is Empty
Before using the `element()` method, it's best to check if the linked list is empty to avoid exceptions:
## Example
if(!myLinkedList.isEmpty()){
String firstElement = myLinkedList.element();
// Process the first element
}else{
// Handle empty list case
}
### 2. Consider Using peek() as an Alternative
If you are unsure whether the linked list might be empty and don't want to handle exceptions, consider using the `peek()` method:
## Example
String firstElement = myLinkedList.peek();
if(firstElement !=null){
// Process the first element
}else{
// Handle empty list case
}
### 3. Performance Considerations
The time complexity of the `element()` method is O(1), as it only accesses the head node of the linked list and does not involve any traversal operations. This has the same performance characteristics as other head operation methods of `LinkedList` (such as `getFirst()`, `peek()`).
* * *
## Summary
The `element()` method of `LinkedList` is a simple but practical tool for safely retrieving the first element of a linked list. Remember its key difference from the `peek()` method and choose the appropriate method based on your specific needs. In cases where you might encounter an empty linked list, either check if the list is empty beforehand or consider using `peek()` to avoid exception handling.
By using these methods appropriately, you can manipulate `LinkedList` collections more effectively and write more robust Java code.
[ Java LinkedList](#)