Java 25 (LTS) reference · List and ArrayList Methods

Java List remove() Method

Remove an element by index or by value. With a List<Integer>, remove(1) removes index 1, not the value 1.

Java 25 (LTS) Since Java 1.0

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

list.remove(index) / list.remove(object)

Parameters

ParameterDescription
index / object
int / Object
The position (int) or the element to remove.

Return value

The removed element, or true/false.

Examples

By value and by index
import java.util.*;

public class Main {
    public static void main(String[] args) {
        List<String> fruits = new ArrayList<>(List.of("apple", "pear", "plum"));
        fruits.remove("pear");
        fruits.remove(0);
        System.out.println(fruits);
    }
}

Output

[plum]

Run this on your computer — see “Get Started” for setup.

The Integer trap and removeIf()
import java.util.*;

public class Main {
    public static void main(String[] args) {
        List<Integer> nums = new ArrayList<>(List.of(5, 9, 7));
        nums.remove(1);                    // an int means index 1, so 9 is removed
        System.out.println(nums);
        nums.remove(Integer.valueOf(7));   // the value 7
        System.out.println(nums);
        List<Integer> evens = new ArrayList<>(List.of(1, 2, 3, 4));
        evens.removeIf(n -> n % 2 != 0);
        System.out.println(evens);
    }
}

Output

[5, 7]
[5]
[2, 4]

Run this on your computer — see “Get Started” for setup.

Version history

VersionChange
Java Java 1.0Added

Was this page helpful?