Java LinkedList removeFirstOccurrence() removeLastOccurrence Tutorial

In this section we will learn what the LinkedList removeFirstOccurrence() and removeLastOccurrence() methods are and how to use them in Java.

What is Java LinkedList removeFirstOccurrence() Method?

The Java LinkedList removeFirstOccurrence method is used to remove the first occurrence of an element in a LinkedList object.

Java removeFirstOccurrence() Method Syntax:

public boolean removeFirstOccurrence(Object o)

removeFirstOccurrence() Method Parameters:

The method takes one argument and that is the element we want to remove its first occurrence from the target LinkedList object.

removeFirstOccurrence() Method Return Value:

The return value of the method is of type Boolean and is true if the operation succeeds (the element was removed). But if the operation failed (for example, because the target list didn’t have the such element in the first place), then the return value will be false.

removeFirstOccurrence() Method Exceptions:

The method does not throw an exception.

Example: using LinkedList removeFirstOccurrence() method

import java.util.LinkedList; 
class Main{
    public static void main(String[] args) {
        LinkedList<String> list = new LinkedList<>();
        list.add("John");
        list.add("Omid");
        list.add("Jack");
        list.add("Omid");
        list.add("Ellen");
        list.add("Elon");
        list.add("Ellen");

        list.removeFirstOccurrence("Omid");

        for (String s: list){
            System.out.println(s);
        }
    }
}

Output:

John

Jack

Omid

Ellen

Elon

Ellen

What is Java LinkedList removeLastOccurrence() Method?

The Java LinkedList removeLastOccurrence() method is used to remove the last occurrence of an element (that we declare as the argument of the method) in a LinkedList object.

Java removeLastOccurrence() Method Syntax:

public boolean removeLastOccurrence(Object o)

removeLastOccurrence() Method Parameters:

The method takes one argument and that is the element we want to remove its last occurrence from the target LinkedList object.

removeLastOccurrence() Method Return Value:

The return value of the method is of type boolean and is true if the operation succeeds (the element was removed). But if the operation failed (for example, because the target list didn’t have the such element in the first place), then the return value will be false.

removeLastOccurrence() Method Exceptions:

The method does not throw an exception.

Example: using LinkedList removeLastOccurrence() method

import java.util.LinkedList; 
class Main{
    public static void main(String[] args) {
        LinkedList<String> list = new LinkedList<>();
        list.add("John");
        list.add("Omid");
        list.add("Jack");
        list.add("Omid");
        list.add("Ellen");
        list.add("Elon");
        list.add("Ellen");

        list.removeLastOccurrence("Ellen");

        for (String s: list){
            System.out.println(s);
        }
    }
}

Output:

John

Omid

Jack

Omid

Ellen

Elon
Facebook
Twitter
Pinterest
LinkedIn

Top Technologies