Introduction
The removeFirstOccurrence()
method in Java's LinkedList
class provides a way to remove the first instance of a specified object from the list. Unlike remove()
which removes the *first* element matching an index, this method finds and removes only the initial occurrence of the target object.
Syntax
public boolean removeFirstOccurrence(Object o)
Parameters
Parameter | Description |
---|---|
o |
The object to be removed from the list. If the list does not contain the specified object, this method returns false . |
Return Value
This method returns true
if the object was found and removed; otherwise, it returns false
.
Examples
Example 1: Removing a String
Let's see how to remove the first occurrence of a specific string from a LinkedList.
import java.util.LinkedList;
public class RemoveFirstOccurrenceExample {
public static void main(String[] args) {
LinkedList<String> myList = new LinkedList<>();
myList.add("apple");
myList.add("banana");
myList.add("orange");
myList.add("banana");
System.out.println("Original list: " + myList);
boolean removed = myList.removeFirstOccurrence("banana");
System.out.println("List after removing first 'banana': " + myList);
System.out.println("Was the element removed? " + removed);
}
}
Original list: [apple, banana, orange, banana]
List after removing first 'banana': [apple, orange, banana]
Was the element removed? true
In this example, we create a LinkedList
of strings. We then call removeFirstOccurrence("banana")
. The list is modified to remove only the *first* instance of "banana". The method returns true
because “banana” was present and removed.
Example 2: Removing an Integer
This example demonstrates removing the first occurrence of an integer from a LinkedList.
import java.util.LinkedList;
public class RemoveFirstOccurrenceIntegerExample {
public static void main(String[] args) {
LinkedList<Integer> myList = new LinkedList<>();
myList.add(10);
myList.add(20);
myList.add(30);
myList.add(20);
System.out.println("Original list: " + myList);
boolean removed = myList.removeFirstOccurrence(20);
System.out.println("List after removing first 20: " + myList);
System.out.println("Was the element removed? " + removed);
}
}
Original list: [10, 20, 30, 20]
List after removing first 20: [10, 30, 20]
Was the element removed? true
Here, we create a LinkedList of Integers and remove the first occurrence of the integer 20. The method correctly removes only the initial '20'. The returned value is true
indicating successful removal.
Example 3: Object Not Found
This example shows what happens when you try to remove an object that doesn't exist in the list.
import java.util.LinkedList;
public class RemoveFirstOccurrenceNotFoundExample {
public static void main(String[] args) {
LinkedList<String> myList = new LinkedList<>();
myList.add("apple");
myList.add("banana");
System.out.println("Original list: " + myList);
boolean removed = myList.removeFirstOccurrence("grape");
System.out.println("List after attempting to remove 'grape': " + myList);
System.out.println("Was the element removed? " + removed);
}
}
Original list: [apple, banana]
List after attempting to remove 'grape': [apple, banana]
Was the element removed? false
In this case, we attempt to remove “grape”, which isn't present in the LinkedList. The list remains unchanged, and removeFirstOccurrence()
returns false
.