Introduction
The getLast()
method in Java's LinkedList
class is a handy tool for retrieving the last element of your linked list without removing it. Think of it like looking at the very end of a train – you want to see what's there, but you don’t want to detach that final car.
Syntax
public E getLast()
Parameters
Parameter | Description |
---|---|
None | This method doesn't accept any parameters. |
Return Value
The getLast()
method returns the last element in the linked list.
- If the list is empty, it throws a
NoSuchElementException
.
Examples
Example 1: Retrieving the Last Element
This example demonstrates how to use getLast()
to get the last element of a LinkedList
.
import java.util.LinkedList;
import java.util.NoSuchElementException;
public class LinkedListLastExample {
public static void main(String[] args) {
LinkedList<String> myList = new LinkedList<>();
myList.add("Apple");
myList.add("Banana");
myList.add("Cherry");
try {
String lastElement = myList.getLast();
System.out.println("The last element is: " + lastElement);
} catch (NoSuchElementException e) {
System.out.println("The list is empty.");
}
}
}
The last element is: Cherry
Explanation: We create a LinkedList
called myList
and add three strings to it. We then call getLast()
, which returns “Cherry”, the last string in the list. A try-catch block handles the case where the list might be empty.
Example 2: Handling an Empty List
This example shows what happens when you attempt to get the last element of an empty linked list and how to handle the resulting exception.
import java.util.LinkedList;
import java.util.NoSuchElementException;
public class LinkedListLastEmptyExample {
public static void main(String[] args) {
LinkedList<Integer> emptyList = new LinkedList<>();
try {
Integer lastElement = emptyList.getLast();
System.out.println("The last element is: " + lastElement); // This line won't be reached.
} catch (NoSuchElementException e) {
System.out.println("Caught NoSuchElementException: The list is empty.");
}
}
}
Caught NoSuchElementException: The list is empty.
Explanation: We create an empty LinkedList
called emptyList
. When we try to retrieve the last element using getLast()
, a NoSuchElementException
is thrown because there's nothing in the list. The catch
block handles this exception and prints an appropriate message.
Example 3: Using with Generic Types
This example demonstrates how to use getLast()
with different data types using generics.
import java.util.LinkedList;
public class LinkedListLastGenericExample {
public static void main(String[] args) {
LinkedList<Double> doubleList = new LinkedList<>();
doubleList.add(3.14);
doubleList.add(2.71);
Double lastDouble = doubleList.getLast();
System.out.println("The last Double element is: " + lastDouble);
LinkedList<Boolean> booleanList = new LinkedList<>();
booleanList.add(true);
booleanList.add(false);
Boolean lastBoolean = booleanList.getLast();
System.out.println("The last Boolean element is: " + lastBoolean);
}
}
The last Double element is: 2.71
The last Boolean element is: false
Explanation: This example shows that the `getLast()` method works with different generic types such as `Double` and `Boolean`. It retrieves the last elements of each list and prints them to the console.