Java LinkedList remove() Method

The LinkedList.remove() methods in Java are used to remove elements from a LinkedList in different ways. This guide will cover the usage of these methods, explain how they work, and provide examples to demonstrate their functionality.

Table of Contents

  1. Introduction
  2. remove() Method Syntax
  3. Examples
    • Removing the First Element
    • Removing an Element by Index
    • Removing an Element by Object
  4. Conclusion

Introduction

The LinkedList.remove() methods are members of the LinkedList class in Java. They allow you to remove elements from the list in various ways, such as by position, by object, or by retrieving and removing the first element. These methods are particularly useful for managing the elements in a LinkedList.

remove() Method Syntax

Removing the First Element

public E remove()
  • Returns: The element that was removed from the list.
  • Throws: NoSuchElementException if the list is empty.

Removing an Element by Index

public E remove(int index)
  • index: The index of the element to be removed.
  • Returns: The element that was removed from the list.
  • Throws: IndexOutOfBoundsException if the index is out of range (index < 0 || index >= size()).

Removing an Element by Object

public boolean remove(Object o)
  • o: The element to be removed from the list, if it is present.
  • Returns: true if the list contained the specified element, false otherwise.

Examples

Removing the First Element

The remove() method can be used to retrieve and remove the first element of a LinkedList.

Example

import java.util.LinkedList;

public class RemoveFirstExample {
    public static void main(String[] args) {
        // Creating a LinkedList of Strings
        LinkedList<String> list = new LinkedList<>();

        // Adding elements to the LinkedList
        list.add("Apple");
        list.add("Banana");
        list.add("Orange");

        // Removing the first element using remove() method
        String removedElement = list.remove();

        // Printing the removed element and the updated LinkedList
        System.out.println("Removed element: " + removedElement);
        System.out.println("Updated LinkedList: " + list);
    }
}

Output:

Removed element: Apple
Updated LinkedList: [Banana, Orange]

Removing an Element by Index

The remove(int index) method can be used to remove an element at a specified position in the LinkedList.

Example

import java.util.LinkedList;

public class RemoveByIndexExample {
    public static void main(String[] args) {
        // Creating a LinkedList of Strings
        LinkedList<String> list = new LinkedList<>();

        // Adding elements to the LinkedList
        list.add("Apple");
        list.add("Banana");
        list.add("Orange");

        // Removing the element at index 1
        String removedElement = list.remove(1);

        // Printing the removed element and the updated LinkedList
        System.out.println("Removed element: " + removedElement);
        System.out.println("Updated LinkedList: " + list);
    }
}

Output:

Removed element: Banana
Updated LinkedList: [Apple, Orange]

Removing an Element by Object

The remove(Object o) method can be used to remove the first occurrence of a specified element from the LinkedList.

Example

import java.util.LinkedList;

public class RemoveByObjectExample {
    public static void main(String[] args) {
        // Creating a LinkedList of Strings
        LinkedList<String> list = new LinkedList<>();

        // Adding elements to the LinkedList
        list.add("Apple");
        list.add("Banana");
        list.add("Orange");

        // Removing the first occurrence of "Banana"
        boolean isRemoved = list.remove("Banana");

        // Printing the result of removal and the updated LinkedList
        System.out.println("Was 'Banana' removed? " + isRemoved);
        System.out.println("Updated LinkedList: " + list);
    }
}

Output:

Was 'Banana' removed? true
Updated LinkedList: [Apple, Orange]

Conclusion

The LinkedList.remove() methods in Java provide different ways to remove elements from a LinkedList. By understanding how to use these methods, you can efficiently manage the elements in your Java applications. Whether you need to remove elements by position, by object, or by retrieving and removing the first element, these methods offer flexible solutions for your tasks. Handling potential exceptions ensures your code remains robust and error-free.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top