The remove() method of the List interface is used to remove elements from a list. Since both ArrayList and LinkedList implement the List interface, they provide two overloaded versions of this method. One removes an element by its value, while the other removes an element at a specified index.
- Automatically shifts the remaining elements after removal.
- Returns a status or the removed element depending on the overloaded method.
import java.util.*;
public class GFG
{
public static void main(String[] args)
{
// Demonstrating remove on ArrayList
List<String> myAlist = new ArrayList<String>();
myAlist.add("Geeks");
myAlist.add("Practice");
myAlist.add("Quiz");
System.out.println("Original ArrayList : " + myAlist);
myAlist.remove("Quiz");
System.out.println("Modified ArrayList : " + myAlist);
// Demonstrating remove on LinkedList
List<String> myLlist = new LinkedList<String>();
myLlist.add("Geeks");
myLlist.add("Practice");
myLlist.add("Quiz");
System.out.println("Original LinkedList : " + myLlist);
myLlist.remove("Quiz");
System.out.println("Modified LinkedList : " + myLlist);
}
}
Output
Original ArrayList : [Geeks, Practice, Quiz] Modified ArrayList : [Geeks, Practice] Original LinkedList : [Geeks, Practice, Quiz] Modified LinkedList : [Geeks, Practice]
Explanation: In the above example, the remove(Object) method removes the first occurrence of "Quiz" from both the ArrayList and LinkedList. Since the element exists in both lists, it is removed successfully and the remaining elements shift one position to the left.
Syntax
public boolean remove(Object obj)
Parameter: obj: The element to be removed from the list.
Returns
trueif the element is found and removed.falseif the element is not present.
Exceptions
- ClassCastException: If the specified object type is incompatible with the list.
- NullPointerException: If null elements are not permitted and the specified object is null.
- UnsupportedOperationException: If the remove operation is not supported.
Example: Using remove(int index) to Remove an Element by Index
import java.util.*;
public class GFG {
public static void main(String[] args) {
// Create an ArrayList
List<String> arrayList = new ArrayList<>();
arrayList.add("Geeks");
arrayList.add("Practice");
arrayList.add("Quiz");
System.out.println("Original ArrayList: " + arrayList);
arrayList.remove(2);
System.out.println("Modified ArrayList: " + arrayList);
// Create a LinkedList
List<String> linkedList = new LinkedList<>();
linkedList.add("Geeks");
linkedList.add("Practice");
linkedList.add("Quiz");
System.out.println("Original LinkedList: " + linkedList);
linkedList.remove(2);
System.out.println("Modified LinkedList: " + linkedList);
}
}
Output
Original ArrayList: [Geeks, Practice, Quiz] Modified ArrayList: [Geeks, Practice] Original LinkedList: [Geeks, Practice, Quiz] Modified LinkedList: [Geeks, Practice]
Explanation:In this example, the remove(int index) method removes the element present at index 2 from both lists. After removal, the remaining elements automatically shift left to fill the vacant position.
Syntax
public E remove(int index)
- Parameter: index: The index of the element to be removed.
- Returns: The element that was removed from the list.
- Exception: IndexOutOfBoundsException – If the specified index is less than 0 or greater than or equal to the list size.
Example: Removing an Integer Value from an ArrayList
import java.util.*;
public class GFG {
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<>();
list.add(10);
list.add(20);
list.add(30);
list.remove(Integer.valueOf(20));
System.out.println(list);
}
}
Output
[10, 30]
Explanation: Since Integer.valueOf(20) creates an Integer object, Java invokes the remove(Object) method and removes the value 20 instead of treating it as an index.
remove(Object) Vs remove(int)
| Feature | remove(Object) | remove(int) |
|---|---|---|
| Removes | Element by value | Element by index |
| Parameter Type | Object | int |
| Return Type | boolean | Removed element |
| If element/index doesn't exist | Returns false | Throws IndexOutOfBoundsException |
| Duplicate Elements | Removes only the first occurrence | Removes the element at the specified index |
Advantages of remove() Method
- Provides two overloaded methods to remove elements either by value or by index.
- Automatically adjusts the list by shifting remaining elements after removal.
- Returns useful information (boolean or the removed element) based on the method used.
- Works consistently with both ArrayList and LinkedList through the List interface.
- Simplifies element deletion without requiring manual index management.
- Supports removal of the first occurrence of a specified element when duplicates are present.