Java ArrayList remove(): Remove a Single Element from List

Java ArrayList.remove() method removes the first occurrence of the specified element from this arraylist if it is present. If the list does not contain the element, the list remains unchanged.

1. Syntax

The remove() method is overloaded and comes in two forms:

  • boolean remove(Object o) – removes the first occurrence of the specified element by value from the list. Returns true is any element is removed from the list, or else false.
  • Object remove(int index) – removes the element at the specified position in this list. Shifts any subsequent elements to the left. Returns the removed element from the list. Throws IndexOutOfBoundsException if the argument index is invalid.

2. Examples to remove an element from ArrayList

2.1. Removing only the First Occurrence of the Element

Java program to remove an object from an ArrayList using remove() method. In the following example, we invoke the remove() method two times.

  • If the element is found in the list, then the first occurrence of the item is removed from the list.
  • Nothing happens if the element is NOT found in the list, and the list remains unchanged.

The following list contains the string “C” twice. The remove() method removes only a single occurrence everytime.

ArrayList<String> alphabets = new ArrayList<>(Arrays.asList("A", "B", "C", "C", "D"));  

alphabets.remove("C");   //[A, B, C, D]

alphabets.remove("C");  //[A, B, D]

alphabets.remove("Z");  //[A, B, D]  - List is unchanged

2.2. Remove All Occurrences of an Element

We cannot directly remove all occurrences of any element from the list using remove() method. We can use removeAll() method for this purpose.

Java program to remove all the occurrences of an object from the ArrayList.

ArrayList<String> alphabets = new ArrayList<>(Arrays.asList("A", "B", "C", "C", "D"));

alphabets.removeAll(Collections.singleton("C"));   //[A, B, D]

3. Remove an Element by Index

While removing an element using the index, we must be very careful about the list size and index argument. Java program to remove an object by its index position from an ArrayList using remove() method.

ArrayList<String> alphabets = new ArrayList<>(Arrays.asList("A", "B", "C", "D"));

alphabets.remove(2);  //Index in range - removes 'C'

alphabets.remove(10);    //Index out of range - IndexOutOfBoundsException

Program output.

[A, B, C, D]
[A, B, D]
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 10, Size: 4
	at java.util.ArrayList.rangeCheck(ArrayList.java:653)
	at java.util.ArrayList.remove(ArrayList.java:492)
	at com.howtodoinjava.example.ArrayListExample.main(ArrayListExample.java:18)

That’s all for the ArrayList remove() method in Java.

Happy Learning !!

Comments

Subscribe
Notify of
guest
2 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial

Dark Mode

Dark Mode