Java LinkedList getLast() Method

Java LinkedList Java LinkedList


getLast()is a method in JavaLinkedListprovided by the class, used to get the last element in the linked list. This method belongs tojava.util.LinkedListthe class library, and is one of the commonly used methods for operating linked list data structures.

Method Definition

public E getLast()
  • Return type: generic E (returns the last element in the linked list)
  • Exception thrown: if the linked list is empty, throwsNoSuchElementException

Use Cases

When you need:

  1. Check the element at the end of the list without removing it
  2. Implement a queue peek operation (view but do not remove the tail element)
  3. In algorithms that require frequent access to elements at the tail of the linked list

Basic Usage Example

Example

import java.util.LinkedList;

public class GetLastExample {
    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 getLast() to get the last element
        String lastFruit = fruits.getLast();
        System.out.println("The last fruit is: " + lastFruit);
    }
}

Output:

最后一个水果是: Cherry

Important Notes

1. Handling an Empty Linked List

Example

LinkedList<Integer> emptyList = new LinkedList<>();
// This line will throw NoSuchElementException
int last = emptyList.getLast();

Safe Usage:

Example

if (!emptyList.isEmpty()) {
    int last = emptyList.getLast();
} else {
    System.out.println("The linked list is empty!");
}

2. Difference from peekLast()

  • getLast(): throws an exception if the linked list is empty
  • peekLast(): returns null when the linked list is empty

Performance Considerations

LinkedListis a doubly linked list implementation,getLast()the time complexity of the method is O(1), because it directly accesses the tail node of the linked list.


Practical Application Example

Implementing a Simple History Feature

Example

LinkedList<String> history = new LinkedList<>();

// Add browsing history
history.add("Home");
history.add("Product Page");
history.add("Shopping Cart");

// Get the most recently visited page
String lastVisited = history.getLast();
System.out.println("The page you last visited is: " + lastVisited);

FAQ

Q1: What is the difference between getLast() and getFirst()?

  • getLast()Get the last element of the linked list
  • getFirst()Get the first element of the linked list

Q2: Why is a separate getLast() method needed?

BecauseLinkedListimplementsDequethe interface, and needs to efficiently support double-ended operations.

Q3: How to avoid getLast() throwing an exception?

  1. First check whether the linked list is empty:list.isEmpty()
  2. UsepeekLast()method instead

Summary

getLast()The method isLinkedLista simple but practical method in the class, which can help developers quickly access the element at the end of the linked list. Understanding the behavior and potential exceptions of this method is very important for writing robust code. In actual development, choose according to specific needsgetLast()orpeekLast()can better handle edge cases.

Java LinkedList Java LinkedList

Other Extensions