Java LinkedList removeFirstOccurrence() Method
Example
Remove the first "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.removeFirstOccurrence("BMW");
System.out.println(cars);
}
}
Definition and Usage
The removeFirstOccurrence() method removes the first occurrence (the first 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 removeLastOccurrence() method to remove the last occurrence of an item.
Syntax
public boolean removeFirstOccurrence(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