Java ArrayList removeLast() Method

The ArrayList.removeLast() method, introduced in Java 21, is used to remove the last element from an ArrayList. This guide will cover the method's usage, explain how it works, and provide examples to demonstrate its functionality.

Table of Contents

  1. Introduction
  2. removeLast Method Syntax
  3. Examples
    • Removing the Last Element
    • Handling Empty ArrayList
  4. Real-World Use Case
  5. Conclusion

Introduction

The removeLast() method is part of the ArrayList class in Java 21. It allows you to remove the last element of the list directly, simplifying the process of removing the last element without needing to handle the index manually.

removeLast Method Syntax

The syntax for the removeLast method is as follows:

public E removeLast()
  • The method returns the element that was removed from the ArrayList.

Examples

Removing the Last Element

The removeLast method can be used to remove the last element of the ArrayList.

Example

import java.util.ArrayList;
import java.util.List;

public class RemoveLastExample {
    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        list.add("Apple");
        list.add("Banana");
        list.add("Orange");

        // Remove the last element
        String removedElement = ((ArrayList<String>) list).removeLast();

        System.out.println("Removed element: " + removedElement);
        System.out.println("List after removal: " + list);
    }
}

Output:

Removed element: Orange
List after removal: [Apple, Banana]

Handling Empty ArrayList

Attempting to remove the last element from an empty ArrayList will throw a NoSuchElementException. It's important to handle this case properly.

Example

import java.util.ArrayList;
import java.util.List;
import java.util.NoSuchElementException;

public class RemoveLastWithExceptionHandling {
    public static void main(String[] args) {
        List<String> list = new ArrayList<>();

        // Remove the last element with exception handling
        try {
            String removedElement = ((ArrayList<String>) list).removeLast();
            System.out.println("Removed element: " + removedElement);
        } catch (NoSuchElementException e) {
            System.out.println("Error: " + e.getMessage());
        }
    }
}

Output:

Error: No elements found in the list

Real-World Use Case

Managing a Browser's History

In a browser, you might want to remove the most recent history entry (the last element) when the user clicks the "Back" button. The removeLast() method can be used to simplify this operation.

Example

import java.util.ArrayList;
import java.util.List;

class HistoryEntry {
    String url;

    HistoryEntry(String url) {
        this.url = url;
    }

    @Override
    public String toString() {
        return url;
    }
}

public class BrowserHistory {
    public static void main(String[] args) {
        List<HistoryEntry> history = new ArrayList<>();
        history.add(new HistoryEntry("http://example.com/page1"));
        history.add(new HistoryEntry("http://example.com/page2"));
        history.add(new HistoryEntry("http://example.com/page3"));

        // Remove the last history entry
        HistoryEntry lastEntry = ((ArrayList<HistoryEntry>) history).removeLast();

        System.out.println("Removed history entry: " + lastEntry);
        System.out.println("Remaining history: " + history);
    }
}

Output:

Removed history entry: http://example.com/page3
Remaining history: [http://example.com/page1, http://example.com/page2]

Conclusion

The ArrayList.removeLast() method in Java 21 provides a convenient way to remove the last element from an ArrayList. By understanding how to use this method, you can efficiently manage the contents of your ArrayList in Java applications. It's important to handle potential NoSuchElementException by ensuring that the list is not empty before attempting to remove the last element. This method is particularly useful in real-world applications such as managing a browser's history.

Comments