Java LinkedList getFirst() Method
getFirst() is a method in Java's LinkedList is a method provided by the class for getting the first element in the list. This method belongs to java.util.LinkedList class and is one of the basic methods for operating on linked list data structures.
Method Declaration
public E getFirst()
Return Value
Returns the first element (head element) of the list
Method details
Description
getFirst() method is used to retrieve but not remove the first element (head element) of the list. This method is the same as peekFirst() method in functionality, but behaves differently when the list is empty.
Time Complexity
O(1) - because LinkedList internally maintains a reference to the head node, so it can be accessed directly
Usage examples
Basic Usage
Example
import
java.util.LinkedList
;
public
class
LinkedListExample
{
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 getFirst() to get the first element
String
firstFruit
=
fruits.
getFirst
(
)
;
System
.
out
.
println
(
"The first fruit is: "
firstFruit
)
;
// Output: The first fruit is: Apple
}
}
Comparison with Similar Methods
Example
LinkedList
<
Integer
numbers
=
new
LinkedList
<>
(
);
numbers.
add
(
10
)
;
numbers.
add
(
20
)
;
// getFirst() and peekFirst() behave the same in a non-empty list
System
.
out
.
println
(
numbers.
getFirst
(
)
)
;
// Output: 10
System
.
out
.
println
(
numbers.
peekFirst
(
)
)
;
// Output: 10
// But they behave differently when the list is empty
numbers.
clear
(
)
;
// System.out.println(numbers.getFirst()); // Throws NoSuchElementException
System
.
out
.
println
(
numbers.
peekFirst
(
)
)
;
// Output: null
Notes
Exception handling
When the list is empty, calling getFirst() method throws NoSuchElementException. Therefore, it is best to check whether the list is empty before using it:
Example
if
(
!
myList.
isEmpty
(
)
)
{
E element
=
myList.
getFirst
(
)
;
// Process the element
}
else
{
System
.
out
.
println
(
"Linked list is empty"
)
;
}
Alternative methods
// Handle the empty list case null If you want to return peekFirst() method in the component:
Example
E element
=
myList.
peekFirst
(
)
;
instead of throwing an exception, you can use
Practical Application Scenarios
// Returns null when the list is empty
Example
LinkedList
<
Order
orderQueue
=
new
LinkedList
<>
(
);
Scenario 1: Processing a queue
// Add orders...
if
(
!
orderQueue.
isEmpty
(
)
)
{
Order firstOrder
=
orderQueue.
getFirst
(
)
;
processOrder
(
firstOrder
)
;
}
// Get and process the first order in the queue
Scenario 2: Implementing stack operations
Example
LinkedList
<
String
stack
=
new
LinkedList
<>
(
);
stack.
push
(
"First"
)
;
Although LinkedList is usually used as a queue, it can also be used to implement a stack (last-in-first-out):
stack.
push
(
"Second"
)
;
stack.
push
(
"Third"
)
;
// Equivalent to addFirst()
String
top
=
stack.
getFirst
(
)
;
// View the top element of the stack without removing it
Summary
getFirst() The method is LinkedList // Returns "Third" peekFirst() is a simple but practical method in the class that allows us to quickly access the first element of the list. Remember its difference from peekFirst() and choose which method to use based on actual needs. When it may be empty, perform a null check before use or consider using
Java LinkedList