Support Online
Skip to main content

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:

MethodParameterDescriptionReturn Value
remove(int index)IndexDeletes the element at the specified index.Deleted element
remove(Object o)ObjectDeletes 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

StatusError CodeSolution
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 Typeremove(int)remove(Object)Description
ArrayListO(n)O(n)Element shifting costs are high
LinkedListO(n) (find) + O(1) (delete)O(n)Advantageous in medium-large data sets
CopyOnWriteArrayListO(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

MethodDescriptionReturnsPossible Error
remove(int)Deletes the element at the specified index.Deleted elementIndexOutOfBoundsException
remove(Object)Deletes the first matching element.true / falseNone
removeIf(Predicate)Deletes all elements that match the condition.true / falseUnsupportedOperationException

📊 Benchmark Comparison (Summary)

MethodArrayListLinkedListDescription
remove(0)Slow (O(n))Fast (O(1))Delete first element
remove(size/2)MediumMediumErase
removeIf()Generally fasterSameConditional bulk delete

❓ Frequently Asked Questions (FAQ)

  1. What is the difference between remove(int) and remove(Object)?

remove(int) deletes by index, remove(Object) deletes by value.

  1. How do I clear the list completely?

list.clear(); The command removes all elements.

  1. What should I do to avoid getting errors during the deletion process?

Use Iterator or prefer removeIf().

  1. Why doesn't remove work on immutable lists?

Because these lists are fixed size and cannot be changed.

  1. 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.