How to Use Java List remove() Method? (ArrayList, LinkedList and Alternatives)
✅ What Will You Learn in This Guide?
This guide explains in detail the different uses of the Java List remove() method (index, value and conditional delete).
Performance differences between ArrayList, LinkedList and CopyOnWriteArrayList; you'll also learn how to avoid common errors such as IndexOutOfBoundsException, UnsupportedOperationException, and ConcurrentModificationException.
Also included are examples of conditional deletion with removeIf() and performance measurement with JMH.
🔍 Login
In Java, the remove() method is used to delete one or more elements from the List structure.
However, this method is overloaded in two ways:
| Method | Parameter | Description | Return Value |
|---|---|---|---|
remove(int index) | Index | Deletes the element at the specified index. | Deleted element |
remove(Object o) | Object | Deletes the first match of the specified object. | true or false |
While ArrayList offers fast access, LinkedList is more efficient for frequent insertions and deletions.
1️⃣ remove(int index) — Deleting Element by Index
import java.util.*;
public class RemoveByIndexExample {
public static void main(String[] args) {
List<String> list = new ArrayList<>(List.of("A", "B", "C", "D"));
String removed = list.remove(1);
System.out.println(list);
System.out.println("Silinen eleman: " + removed);
}
}
🧠 In this example, index 1 (i.e. "B") is deleted and the list is updated to [A, C, D].
⚠️ IndexOutOfBoundsException
If an invalid index (e.g. 10) is given, an error occurs:
List<String> list = new ArrayList<>(List.of("A"));
list.remove(10); // Hata!
✅ Solution:
if (i >= 0 && i < list.size()) list.remove(i);
2️⃣ remove(Object o) — Deleting Element by Value
import java.util.*;
public class RemoveByValueExample {
public static void main(String[] args) {
List<String> list = new ArrayList<>(List.of("A", "B", "C", "B"));
list.remove("B");
System.out.println(list);
}
}
🧠 This method deletes only the first matching value. If there is no element, the list does not change and returns false.
3️⃣ removeIf() — Conditional Delete (Java 8+)
list.removeIf(e -> e.startsWith("temp_"));
🧩 This method deletes elements in bulk based on a condition. removeIf() is generally faster than manual loops because it is optimized by the JVM.
4️⃣ Delete Error in Immutable Lists
Some lists (List.of(), Arrays.asList()) are immutable and throw UnsupportedOperationException when remove() is called.
List<String> immutableList = List.of("A", "B");
immutableList.remove("A"); // ❌ Hata
✅ Solution:
List<String> mutable = new ArrayList<>(immutableList);
mutable.remove("A"); // ✔️ Çalışır
🧩 Summary Table
| Status | Error Code | Solution |
|---|---|---|
Arrays.asList() | Arrays.asList("A", "B").add("C"); ❌ | new ArrayList<>(Arrays.asList("A", "B")) ✅ |
List.of() (Java 9+) | List.of("X", "Y").add("Z"); ❌ | new ArrayList<>(List.of("X", "Y")) ✅ |
5️⃣ Common Mistakes When Using remove()
🚫 ConcurrentModificationException Deleting in for-each causes error:
for (String item : list) {
list.remove(item); // ❌
}
✅ Solution: Use Iterator:
Iterator<String> it = list.iterator();
while (it.hasNext()) {
if (it.next().equals("B")) it.remove();
}
⚙️ remove() Performance by List Types
| List Type | remove(int) | remove(Object) | Description |
|---|---|---|---|
| ArrayList | O(n) | O(n) | Element shifting costs are high |
| LinkedList | O(n) (find) + O(1) (delete) | O(n) | Advantageous in medium-large data sets |
| CopyOnWriteArrayList | O(n) | O(n) | Thread-safe but copies on every delete |
💡 Rule:
If you want quick access → ArrayList
If there are frequent deletions/additions → LinkedList
Thread-safe, low-variance environments → CopyOnWriteArrayList
🧮 remove() vs removeIf() Comparison
| Method | Description | Returns | Possible Error |
|---|---|---|---|
remove(int) | Deletes the element at the specified index. | Deleted element | IndexOutOfBoundsException |
remove(Object) | Deletes the first matching element. | true / false | None |
removeIf(Predicate) | Deletes all elements that match the condition. | true / false | UnsupportedOperationException |
📊 Benchmark Comparison (Summary)
| Method | ArrayList | LinkedList | Description |
|---|---|---|---|
remove(0) | Slow (O(n)) | Fast (O(1)) | Delete first element |
remove(size/2) | Medium | Medium | Erase |
removeIf() | Generally faster | Same | Conditional bulk delete |
❓ Frequently Asked Questions (FAQ)
- What is the difference between remove(int) and remove(Object)?
remove(int) deletes by index, remove(Object) deletes by value.
- How do I clear the list completely?
list.clear(); The command removes all elements.
- What should I do to avoid getting errors during the deletion process?
Use Iterator or prefer removeIf().
- Why doesn't remove work on immutable lists?
Because these lists are fixed size and cannot be changed.
- Which list should I use in multithread environments?
Prefer CopyOnWriteArrayList or Collections.synchronizedList().
🧠 Conclusion
Although the remove() method in Java seems like a simple delete function, its performance varies significantly depending on the list type and intended use. Choosing the right method is critical for both efficiency and error management.

