Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Java

Java List.remove: removing by index or by value

Choose the right `List.remove` overload: one returns the element removed at an index, while the other reports whether it removed the first matching value.

Version: Java SE 21

Syntax

E remove(int index)
boolean remove(Object o)

List.remove(int) removes the element at a zero-based index and returns that element as E. Each later element's index decreases by one. On a list that supports removal, a negative index or an index at least size() causes IndexOutOfBoundsException.

List.remove(Object) removes only the first element for which Objects.equals(o, element) is true. It returns true if it removed an element, or false if there was no match and the list stayed unchanged. Both overloads are optional operations: a list that does not support removal can throw UnsupportedOperationException. The Object overload may also throw ClassCastException for an incompatible type or NullPointerException for null if the list disallows null elements.

The compiler selects the overload from the argument's compile-time type. For a List<Integer>, numbers.remove(1) removes the element at index 1, whereas numbers.remove(Integer.valueOf(1)) looks for the value 1. The second call can pass its argument as Object without the unboxing that an int parameter would require.

subList(from, to) returns a view of the range [from, to), not a copy. On a valid view that supports removal, window.remove(0) removes the view's first element from the backing list too. After a structural change to the backing list outside the view, such as a direct add, the view's behavior is undefined. Calling window.remove(0) on such an ArrayList view can throw ConcurrentModificationException even in a single thread; List does not guarantee that exception. Create a fresh view, or make an independent copy before the change.

Examples

import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        List<Integer> numbers = new ArrayList<>(List.of(1, 7, 1, 9, 1));

        Integer removed = numbers.remove(1);
        System.out.println("index: " + removed + " -> " + numbers);

        boolean found = numbers.remove(Integer.valueOf(1));
        System.out.println("value: " + found + " -> " + numbers);

        boolean missing = numbers.remove(Integer.valueOf(42));
        System.out.println("missing: " + missing + " -> " + numbers);

        List<Integer> window = numbers.subList(1, 3);
        Integer fromView = window.remove(0);
        System.out.println("view: " + fromView + " -> " + numbers);
    }
}

Result

index: 7 -> [1, 1, 9, 1]
value: true -> [1, 9, 1]
missing: false -> [1, 9, 1]
view: 9 -> [1, 1]

Index `1` holds `7`. The next call removes just the first `1`, a missing `42` gives `false`, and removing `9` through the view updates the backing list.

Related lessons

  • Java · Lesson 21 · Advanced · Version: 2
  • Java · Lesson 54 · Basic · Version: 2

Sources