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
Syntax
list.remove(index) / list.remove(object)
Parameters
| Parameter | Description |
|---|---|
index / objectint / Object |
The position (int) or the element to remove. |
Return value
The removed element, or true/false.
Examples
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.
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
| Version | Change |
|---|---|
| Java Java 1.0 | Added |
Related
list.add()· Append an element to the end of a list, or insert it at an index, shifting later elements right.list.contains()· Return true if the list holds an element equal to the given one (using equals()). O(n) for ArrayList.- Java ArrayList · tutorial