← 输入框|www.baeldung.com|刷新
Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Black Friday 2026 – NPI EA – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

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.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Black Friday 2026 – NPI EA – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW