Java Reference Library →

LinkedList removeFirstOccurrence() method - Syntax and Examples

LinkedList removeFirstOccurrence() method - Syntax and Examples

Learn about the Java LinkedList `removeFirstOccurrence()` method, its syntax, parameters, return value, and practical examples.

This reference explains LinkedList removeFirstOccurrence() method - Syntax and Examples through its syntax, examples and boundaries. Use the linked course lessons when the surrounding Java concepts are unfamiliar.

  1. Use this reference

    Identify the documented behavior, read a complete example and check its inputs before applying it.

LinkedList removeFirstOccurrence() method - Syntax and Examples

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.

Using this reference

Check the example’s types, inputs and documented behavior before copying it into an application. Some APIs require external libraries, files or platform services; consult their setup notes.

Related explanations: Java Data Types - Primitive and Non-Primitive, Java Class - Blueprint of Objects, Exceptions in Java Introduction.

Supporting documentation: Official Java learning guide.

Reference checks are optional and do not count toward course progress.

    Reference library →