1. Overview

In this tutorial, we’re going to see how to remove elements from an ArrayList in Java using different techniques. Let’s suppose we have a method providing a list of sports:

List<String> getSportList() {
    List<String> sports = new ArrayList<>();
    sports.add("Football");
    sports.add("Basketball");
    sports.add("Baseball");
    sports.add("Boxing");
    sports.add("Cycling");
    return sports;
}

Next, let’s see how to remove some elements from the sport List.

2. ArrayList#remove

ArrayList has two available methods to remove an element, passing the index of the element to be removed, or passing the element itself to be removed, if present. We’re going to see both usages.

2.1. Remove by Index

Using remove passing an index as parameter, we can remove the element at the specified position in the List and shift any subsequent elements to the left, subtracting one from their indices. After execution, the remove method will return the element that has been removed:

List<String> sports = getSportList();
assertThat(sports).hasSize(5).contains("Basketball");
 
sports.remove(1); //removing Basketball
assertThat(sports).hasSize(4).doesNotContain("Basketball");

2.2. Remove by Element

Another way is to remove the first occurrence of an element from a List using this method. Formally speaking, we’re removing the element with the lowest index if exists, if not, the List is unchanged:

List<String> sports = getSportList();
assertThat(sports).hasSize(5).contains("Basketball");
 
sports.remove("Basketball");
 
aassertThat(sports).hasSize(4).doesNotContain("Basketball");

2.3. Remove the Last Element

We often need to remove the last element from a List in our daily work. We can first compute the index of the last element and apply the “remove by index” approach to achieve it:

List<String> sports = getSportList();
List<String> expected = List.of("Football", "Basketball", "Baseball", "Boxing");
 
sports.remove(sports.size() - 1);
assertThat(sports).isEqualTo(expected);

In this example, we calculate the index of the last element using list.size() -1.

Since JDK 21, a new method has joined the List family: removeLast(). As its name implies, it removes the last element from a List:

List<String> sports = getSportList();
List<String> expected = List.of("Football", "Basketball", "Baseball", "Boxing");
 
sports.removeLast();
assertThat(sports).isEqualTo(expected);

As we can see, removeLast() is pretty straightforward to use and easier to understand. Therefore, if we’re using JDK 21 or higher, this method can be a good option for removing the last element from a List.

3. Removing While Iterating

Sometimes, we want to remove an element from an ArrayList while we’re looping it. Due to not generating a ConcurrentModificationException, we need to use the Iterator class to do it properly.

Let’s see how we can get rid of an element in a loop:

List<String> sports = getSportList();
assertThat(sports).hasSize(5).contains("Basketball");
 
Iterator<String> iterator = sports.iterator();
while (iterator.hasNext()) {
    if (iterator.next().equals("Basketball")) {
        iterator.remove();
        break;
    }
}
assertThat(sports).hasSize(4).doesNotContain("Basketball");

4. ArrayList#removeIf (JDK 8+)

If we’re using JDK 8 or higher versions, we can take advantage of *ArrayList#*removeIf which removes all of the elements of the ArrayList that satisfy a given predicate:

List<String> sports = getSportList();
assertThat(sports).hasSize(5).contains("Basketball");

sports.removeIf(p -> p.equals("Basketball"));
 
assertThat(sports).hasSize(4).doesNotContain("Basketball");

Finally, we can do it using third party libraries like Apache Commons and, if we want to go deeper, we can see how to remove all specific occurrences in an efficient way.

5. Conclusion

In this article, we looked at the various ways of removing elements from an ArrayList in Java.

As usual, all the examples used in this tutorial are available over on GitHub.