Introduction
The removeLastOccurrence()
method in Java's LinkedList
class provides a convenient way to remove the last occurrence of a specified element from the list. It searches through the entire list and removes only the *last* instance of the element you provide, leaving earlier occurrences untouched.
Syntax
public boolean removeLastOccurrence(Object o)
Parameters
Parameter | Description |
---|---|
o |
The element to be removed from the list. If the list does not contain the specified element, this method returns false . |
Return Value
This method returns true
if the element was found and removed successfully. It returns false
if the list does not contain the specified element.
Examples
Example 1: Removing a Single Occurrence
This example demonstrates removing the last occurrence of an integer '5' from a LinkedList.
import java.util.LinkedList;
public class RemoveLastOccurrenceExample {
public static void main(String[] args) {
LinkedList<Integer> list = new LinkedList<>();
list.add(1);
list.add(2);
list.add(5);
list.add(3);
list.add(5);
System.out.println("Original List: " + list);
boolean removed = list.removeLastOccurrence(5);
System.out.println("List after removing last occurrence of 5: " + list);
System.out.println("Was the element removed? " + removed);
}
}
Original List: [1, 2, 5, 3, 5]
List after removing last occurrence of 5: [1, 2, 5, 3]
Was the element removed? true
In this example, only the second '5' is removed from the list. The first '5' remains.
Example 2: Element Not Present
This example shows what happens when you try to remove an element that isn't in the LinkedList.
import java.util.LinkedList;
public class RemoveLastOccurrenceNotFoundExample {
public static void main(String[] args) {
LinkedList<String> list = new LinkedList<>();
list.add("apple");
list.add("banana");
list.add("cherry");
System.out.println("Original List: " + list);
boolean removed = list.removeLastOccurrence("grape");
System.out.println("List after attempting to remove 'grape': " + list);
System.out.println("Was the element removed? " + removed); // Output will be false
}
}
Original List: [apple, banana, cherry]
List after attempting to remove 'grape': [apple, banana, cherry]
Was the element removed? false
Since “grape” isn't in the list, removeLastOccurrence()
returns false
and the list remains unchanged.
Example 3: Removing from a List of Strings
This example illustrates removing the last occurrence of a string element from a LinkedList containing strings.
import java.util.LinkedList;
public class RemoveLastOccurrenceStringExample {
public static void main(String[] args) {
LinkedList<String> list = new LinkedList<>();
list.add("red");
list.add("green");
list.add("blue");
list.add("green");
System.out.println("Original List: " + list);
boolean removed = list.removeLastOccurrence("green");
System.out.println("List after removing last occurrence of 'green': " + list);
System.out.println("Was the element removed? " + removed);
}
}
Original List: [red, green, blue, green]
List after removing last occurrence of 'green': [red, green, blue]
Was the element removed? true
Only the second “green” is removed. The first remains.
Example 4: Empty List
This example demonstrates behavior when calling removeLastOccurrence on an empty list.
import java.util.LinkedList;
public class RemoveLastOccurrenceEmptyList {
public static void main(String[] args) {
LinkedList list = new LinkedList<>();
System.out.println("Original List: " + list);
boolean removed = list.removeLastOccurrence(10); // Try to remove 10 from an empty list
System.out.println("List after attempting to remove 10 (empty): " + list);
System.out.println("Was the element removed? " + removed);
}
}
Original List: []
List after attempting to remove 10 (empty): []
Was the element removed? false
As expected, the list remains empty and the method returns false since there's nothing to remove.