LinkedList getFirst() method - Syntax and Examples
Java LinkedList getFirst() method - Syntax and Examples
This reference explains LinkedList getFirst() method - Syntax and Examples through its syntax, examples and boundaries. Use the linked course lessons when the surrounding Java concepts are unfamiliar.
Use this reference
Identify the documented behavior, read a complete example and check its inputs before applying it.
LinkedList getFirst() method - Syntax and Examples
Java LinkedList getFirst() method
The getFirst() method in Java's LinkedList class is a handy tool for retrieving the first element of the list without removing it. Think of it like peeking at the top card of a deck – you see what’s there, but the card stays put.
Syntax
public E getFirst()
Description
This method returns the first element of this list. It does not modify the list; the first element remains in its original position.
Parameters
| Parameter | Description |
|---|
Return Value
| Return Type | Description |
|---|---|
E |
The first element in the LinkedList. Returns null if the list is empty. |
Examples
Example 1: Retrieving the First Element
This example demonstrates how to use getFirst() to retrieve the first element from a LinkedList containing strings.
import java.util.LinkedList;
public class GetFirstExample {
public static void main(String[] args) {
LinkedList<String> list = new LinkedList<>("Apple", "Banana", "Cherry");
if (!list.isEmpty()) {
String firstElement = list.getFirst();
System.out.println("The first element is: " + firstElement);
} else {
System.out.println("The list is empty.");
}
}
}
The first element is: Apple
Explanation: We create a LinkedList containing three strings. The getFirst() method retrieves "Apple", the first string in the list, and stores it in the firstElement variable. Finally, we print the value of this variable.
Example 2: Handling an Empty List
This example shows what happens when you try to get the first element from an empty LinkedList.
import java.util.LinkedList;
public class GetFirstEmptyListExample {
public static void main(String[] args) {
LinkedList<Integer> list = new LinkedList<>; // Empty List
if (!list.isEmpty()){
Integer firstElement = list.getFirst();
System.out.println("The first element is: " + firstElement);
} else {
System.out.println("The list is empty.");
}
}
}
The list is empty.
Explanation: Here, we create an empty LinkedList. The isEmpty() check prevents the code from attempting to get an element from a non-existent one. Because the list is empty, the program prints "The list is empty.". Attempting to call getFirst() on an empty list would throw a `NoSuchElementException` if the isEmpty check wasn't present.
Example 3: Using with Generic Types
This example demonstrates that getFirst() works seamlessly with LinkedLists of any type.
import java.util.LinkedList;
public class GetFirstGenericExample {
public static void main(String[] args) {
LinkedList<Double> list = new LinkedList<>(1.1, 2.2, 3.3);
if (!list.isEmpty()) {
Double firstElement = list.getFirst();
System.out.println("The first element is: " + firstElement);
} else {
System.out.println("The list is empty.");
}
}
}
The first element is: 1.1
Explanation: This example creates a LinkedList containing Double values. The getFirst() method correctly retrieves the first value (1.1) and prints it to the console.
Using this reference
Check the example’s types, inputs and documented behavior before copying it into an application. Some APIs require external libraries, files or platform services; consult their setup notes.
Related explanations: Java Data Types - Primitive and Non-Primitive, Java Class - Blueprint of Objects, Exceptions in Java Introduction.
Supporting documentation: Official Java learning guide.
Reference checks are optional and do not count toward course progress.