Java LinkedList removeLastOccurrence() Method

The LinkedList.removeLastOccurrence() method in Java is used to remove the last occurrence of a specified element from the LinkedList. 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. removeLastOccurrence Method Syntax
  3. Examples
    • Removing the Last Occurrence of an Element
    • Handling Non-Existent Elements
  4. Conclusion

Introduction

The LinkedList.removeLastOccurrence() method is a member of the LinkedList class in Java. It allows you to remove the last occurrence of a specified element from the LinkedList. This method is particularly useful when you want to remove an element based on its value rather than its position in the list.

removeLastOccurrence Method Syntax

The syntax for the removeLastOccurrence method is as follows:

public boolean removeLastOccurrence(Object o)
  • o: The element to be removed from the list, if it is present.
  • Returns: true if the list contained the specified element, false otherwise.

Examples

Removing the Last Occurrence of an Element

The removeLastOccurrence method can be used to remove the last occurrence of a specified element from a LinkedList.

Example

import java.util.LinkedList;

public class RemoveLastOccurrenceExample {
    public static void main(String[] args) {
        // Creating a LinkedList of Strings
        LinkedList<String> list = new LinkedList<>();

        // Adding elements to the LinkedList
        list.add("Apple");
        list.add("Banana");
        list.add("Orange");
        list.add("Banana");

        // Removing the last occurrence of "Banana"
        boolean isRemoved = list.removeLastOccurrence("Banana");

        // Printing the result of removal and the updated LinkedList
        System.out.println("Was 'Banana' removed? " + isRemoved);
        System.out.println("Updated LinkedList: " + list);
    }
}

Output:

Was 'Banana' removed? true
Updated LinkedList: [Apple, Banana, Orange]

Handling Non-Existent Elements

If the specified element is not found in the LinkedList, the removeLastOccurrence method returns false.

Example

import java.util.LinkedList;

public class RemoveLastOccurrenceNonExistentExample {
    public static void main(String[] args) {
        // Creating a LinkedList of Strings
        LinkedList<String> list = new LinkedList<>();

        // Adding elements to the LinkedList
        list.add("Apple");
        list.add("Banana");
        list.add("Orange");

        // Attempting to remove the last occurrence of "Grapes"
        boolean isRemoved = list.removeLastOccurrence("Grapes");

        // Printing the result of removal and the updated LinkedList
        System.out.println("Was 'Grapes' removed? " + isRemoved);
        System.out.println("Updated LinkedList: " + list);
    }
}

Output:

Was 'Grapes' removed? false
Updated LinkedList: [Apple, Banana, Orange]

Conclusion

The LinkedList.removeLastOccurrence() method in Java provides a way to remove the last occurrence of a specified element from a LinkedList. By understanding how to use this method, you can efficiently manage collections of objects in your Java applications. Handling cases where the element is not found ensures your code remains robust and error-free.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top