Java LinkedList removeLastOccurrence() Method
Example
Remove the last "BMW" from a list:
import java.util.LinkedList;
public class Main {
public static void main(String[] args) {
LinkedList<String> cars = new LinkedList<String>();
cars.add("Volvo");
cars.add("BMW");
cars.add("Ford");
cars.add("BMW");
cars.removeLastOccurrence("BMW");
System.out.println(cars);
}
}
Definition and Usage
The removeLastOccurrence() method removes the last occurrence (the last place where it appears) of an item from the list.
If the item is not in the list, the list does not change.
Tip: Use the removeFirstOccurrence() method to remove the first occurrence of an item.
Syntax
public boolean removeLastOccurrence(Object item)
Parameter Values
| Parameter | Description |
|---|---|
| item | Required. The item to remove from the list. |
Technical Details
| Returns: | true if the item was found and removed, otherwise false. |
|---|
Related Pages
❮ LinkedList Methods