Java LinkedList remove() Method
remove()The method is a JavaLinkedListimportant method in the class, used to remove elements from the linked list. LinkedList is part of the Java collection framework and implements the List and Deque interfaces.
remove()The method has several different forms, each with a specific purpose:
E remove(int index) // Removes the element at the specified position
boolean remove(Object o) // Removes the first occurrence of the specified element
Method Details
remove() - No-argument Form
Example
Function: Removes and returns the first element of this list.
Parameters: None
Return Value: The removed element
Exceptions: Throws if the list is emptyNoSuchElementException
Example:
Example
list.add("Apple");
list.add("Banana");
list.add("Cherry");
String first = list.remove(); // Removes and returns "Apple"
System.out.println("Removed: " + first); // Output: Removed: Apple
System.out.println(list); // Output: [Banana, Cherry]
remove(int index) - With Index Parameter
Example
Function: Removes the element at the specified position in the list.
Parameters:
index- The index of the element to be removed
Return Value: The removed element
Exceptions:
IndexOutOfBoundsException- If the index is out of range (index < 0 || index >= size())
Example:
Example
numbers.add(10);
numbers.add(20);
numbers.add(30);
int removed = numbers.remove(1); // Removes the element at index 1 (20)
System.out.println("Removed: " + removed); // Output: Removed: 20
System.out.println(numbers); // Output: [10, 30]
remove(Object o) - With Object Parameter
Example
Function: Removes the first occurrence of the specified element from the list, if it exists.
Parameters:
o- The element to be removed from the list (if it exists)
Return Value:
true- If the list contains the specified elementfalse- If the list does not contain the specified element
Example:
Example
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Apple");
boolean result = fruits.remove("Apple"); // Removes the first "Apple"
System.out.println("Removed? " + result); // Output: Removed? true
System.out.println(fruits); // Output: [Banana, Apple]
Method Comparison
| Method Form | Parameter Type | Return Value | Exceptions | Purpose |
|---|---|---|---|---|
remove() |
None | E (Element Type) | NoSuchElementException | Removes and returns the first element |
remove(int index) |
int | E (Element Type) | IndexOutOfBoundsException | Removes the element at the specified position |
remove(Object o) |
Object | boolean | None | Removes the first occurrence of the specified element |
Usage Notes
Performance Considerations
LinkedList'sremove()method performance in different scenarios:
- Removing the head element (
remove()orremove(0)): O(1) time complexity - Removing the tail element (
remove(size()-1)): O(1) time complexity - Removing an element in the middle: O(n) time complexity, because it needs to traverse to the specified position
Concurrent Modification
When iterating over a LinkedList with an iterator, callingremove()method may causeConcurrentModificationException. The correct approach is to use the iterator's ownremove()method.
Incorrect Example:
Example
for (Integer num : list) {
if (num == 2) {
list.remove(num); // May throw ConcurrentModificationException
}
}
Correct Approach:
Example
while (it.hasNext()) {
Integer num = it.next();
if (num == 2) {
it.remove(); // Use the iterator's remove() method
}
}
Handling null Values
LinkedList allows storing null values. You can useremove(null)to remove null elements from the list.
Example
list.add("A");
list.add(null);
list.add("B");
list.remove(null); // Removes null elements
System.out.println(list); // Output: [A, B]
Practical Application Examples
Implementing a Queue
LinkedList'sremove()method can be used to implement the FIFO (First-In-First-Out) characteristic of a queue:
Example
queue.add("Task1");
queue.add("Task2");
queue.add("Task3");
while (!queue.isEmpty()) {
String task = queue.remove(); // Removes and returns the first element
System.out.println("Processing: " + task);
}
Removing Elements Matching Specific Conditions
Example
// Removes all even numbers
numbers.removeIf(n -> n % 2 == 0);
System.out.println(numbers); // Output: [1, 3, 5]
Comparison with ArrayList's remove()
Example
LinkedList<Integer> linkedList = new LinkedList<>(Arrays.asList(1, 2, 3, 4, 5));
linkedList.remove(2); // Relatively efficient
// ArrayList's remove() requires shifting elements at middle positions
ArrayList<Integer> arrayList = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));
arrayList.remove(2); // Needs to shift subsequent elements
Summary
LinkedList'sremove()method provides multiple forms to meet different removal needs:
remove()- Quickly removes the head element, suitable for queue operationsremove(int index)- Precisely controls the position of the element to be removedremove(Object o)- Removes based on the element value
In practical applications, you should choose the appropriate method form based on specific needs, and pay attention to their performance characteristics and exceptions. Understanding the differences and applicable scenarios of these methods will help you use LinkedList, an important collection class, more effectively.
Other Extensions
Java LinkedList