Yandex

Java Advanced ConceptsJava Advanced Concepts3

Java LinkedList remove() method
Syntax and Examples


Introduction

The remove() methods in Java's LinkedList class provide ways to eliminate elements from the list. This tutorial explores three variations: remove() (removes the first element), remove(int index) (removes an element at a specific index), and remove(Object o) (removes the first occurrence of a specified object).

remove() - Removing the First Element

This overload removes and returns the *first* element in the linked list. If the list is empty, it throws a NoSuchElementException.

class Main {
  public static void main(String[] args) {
    LinkedList<String> myList = new LinkedList<>("Apple", "Banana", "Cherry");

    try {
      String firstElement = myList.remove();
      System.out.println("Removed element: " + firstElement);
      System.out.println("Remaining list: " + myList);
    } catch (NoSuchElementException e) {
      System.out.println("List is empty.");
    }
  }
}
Removed element: Apple
Remaining list: [Banana, Cherry]

Explanation: The code initializes a LinkedList with three string elements. Then, it calls the remove() method which removes and returns "Apple", the first element of the list. The remaining list is then printed to the console.

remove(int index) - Removing an Element at a Specific Index

This version removes and returns the element at the specified index within the linked list. The index must be valid (between 0 and size of the list minus one, inclusive). If the index is out of bounds, it throws an IndexOutOfBoundsException.

class Main {
  public static void main(String[] args) {
    LinkedList<Integer> myList = new LinkedList<>(10, 20, 30, 40);

    try {
      Integer removedElement = myList.remove(1); // Remove element at index 1 (which is 20)
      System.out.println("Removed element: " + removedElement);
      System.out.println("Remaining list: " + myList);
    } catch (IndexOutOfBoundsException e) {
      System.out.println("Invalid index.");
    }
  }
}
Removed element: 20
Remaining list: [10, 30, 40]

Explanation: This example creates a linked list of integers. We call remove(1) to remove the element at index 1 (the second element), which is 20. The removed element and the updated list are then printed.

remove(Object o) - Removing the First Occurrence of an Object

This overload removes the *first* occurrence of the specified object from the linked list. If the object isn't found, it returns false; otherwise, it returns true.

class Main {
  public static void main(String[] args) {
    LinkedList<String> myList = new LinkedList<>("Apple", "Banana", "Cherry", "Banana");

    boolean removed = myList.remove("Banana");
    System.out.println("Removed 'Banana'? " + removed);
    System.out.println("Remaining list: " + myList);
  }
}
Removed 'Banana'? true
Remaining list: [Apple, Cherry, Banana]

Explanation: Here we create a linked list containing two occurrences of the string "Banana". The call remove("Banana") removes only the *first* occurrence of this string. The method returns true to indicate that an element was removed, and the remaining list is printed.

Parameter Description
None (for remove()) Removes and returns the first element of the list.
index (for remove(int index)) The index of the element to be removed. Must be within the valid range [0, size - 1].
o (for remove(Object o)) The object to be removed from the list. Only the first occurrence is removed.
Return Value Description
None (for remove()) Returns the removed element. Throws NoSuchElementException if list is empty.
Element at index (for remove(int index)) Returns the removed element. Throws IndexOutOfBoundsException if index is invalid.
boolean (for remove(Object o)) True if an element was removed, false otherwise.

Welcome to ProgramGuru

Sign up to start your journey with us

Support ProgramGuru.org

You can support this website with a contribution of your choice.

When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.

PayPal

UPI

PhonePe QR

MALLIKARJUNA M