Use an Iterator instead and use Iterator#remove method:

for (Iterator<String> it = a.iterator(); it.hasNext(); ) {
    String str = it.next();
    if (!str.equals("foo") || !str.equals("bar")) {
        it.remove();
    }
}

From your question:

messing with for iterators is not really good practice

In fact, if you code oriented to interfaces and use List instead of ArrayList directly, using get method could become into navigating through all the collection to get the desired element (for example, if you have a List backed by a single linked list). So, the best practice here would be using iterators instead of using get.

what is the best practice to remove items from a list efficiently?

Not only for Lists, but for any Collection that supports Iterable, and assuming you don't have an index or some sort of key (like in a Map) to directly access to an element, the best way to remove an element would be using Iterator#remove.

Answer from Luiggi Mendoza on Stack Overflow
๐ŸŒ
W3Schools
w3schools.com โ€บ java โ€บ ref_arraylist_remove.asp
Java ArrayList remove() Method
Remove an integer from the list by position and by value: import java.util.ArrayList; public class Main { public static void main(String[] args) { ArrayList<Integer> list = new ArrayList<Integer>(); list.add(5); list.add(8); list.add(9); list.add(1); list.remove(Integer.valueOf(1)); // Remove by object list.remove(1); // Remove by index System.out.println(list); } } Try it Yourself ยป ยท
Discussions

Java how to remove element from List efficiently - Stack Overflow
31 How to efficiently (performance) remove many items from List in Java? More on stackoverflow.com
๐ŸŒ stackoverflow.com
List.remove()
Another option beyond those already given is .removeIf(o -> o == objectToRemove) More on reddit.com
๐ŸŒ r/java
47
50
October 23, 2025
java - Removing items from a list - Stack Overflow
While looping through a list, I would like to remove an item of a list depending on a condition. See the code below. This gives me a ConcurrentModification exception. for (Object a : list) { ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Question and solution: How to delete or add an object to/from array list while iterating it.
By iterating with a classic for ... to/from the end of the list wont effect the loop. So it should be: ... I was talking with u/AgardenerCoding about the problem. And he encouraged me to post this as it may be very useful for someone else searching for an answer to same problem. ... See also the retainAll and removeAll methods in the JavaDoc... More on reddit.com
๐ŸŒ r/processing
7
9
December 28, 2022
๐ŸŒ
How to do in Java
howtodoinjava.com โ€บ home โ€บ collections framework โ€บ java arraylist โ€บ java arraylist remove(): remove a single element from list
Java ArrayList remove(): Remove a Single Element from List with Examples - HowToDoInJava
August 7, 2023 - Returns the removed element from the list. Throws IndexOutOfBoundsException if the argument index is invalid. Java program to remove an object from an ArrayList using remove() method.
Top answer
1 of 4
3

Use an Iterator instead and use Iterator#remove method:

for (Iterator<String> it = a.iterator(); it.hasNext(); ) {
    String str = it.next();
    if (!str.equals("foo") || !str.equals("bar")) {
        it.remove();
    }
}

From your question:

messing with for iterators is not really good practice

In fact, if you code oriented to interfaces and use List instead of ArrayList directly, using get method could become into navigating through all the collection to get the desired element (for example, if you have a List backed by a single linked list). So, the best practice here would be using iterators instead of using get.

what is the best practice to remove items from a list efficiently?

Not only for Lists, but for any Collection that supports Iterable, and assuming you don't have an index or some sort of key (like in a Map) to directly access to an element, the best way to remove an element would be using Iterator#remove.

2 of 4
2

You have three main choices:

  1. Use an Iterator, since it has that handy remove method on it. :-)

    Iterator<String> it = list.iterator();
    while (it.hasNext()) {
        if (/*...you want to remove `it.next()`...*/) {
            it.remove();
        }
    }
    
  2. Loop backward through the list, so that if you remove something, it doesn't matter for the next iteration. This also has the advantage of only calling list.size() once.

    for (int index = list.size() - 1; index >= 0; --index) {
        // ...check and optionally remove here...
    }
    
  3. Use a while loop instead, and only increment the index variable if you don't remove the item.

    int index = 0;
    while (index < list.size()) {
        if (/*...you want to remove the item...*/) {
            list.removeAt(index);
        } else {
            // Not removing, move to the next
            ++index;
        }
    }
    

Remember that unless you know you're dealing with an ArrayList, the cost of List#get(int) may be high (it may be a traversal). But if you know you're dealing with ArrayList (or similar), then...

๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ how-to-remove-a-sublist-from-a-list-in-java
How to remove a SubList from a List in Java - GeeksforGeeks
July 11, 2025 - Input list = [1, 2, 3, 4, 5, 6, 7, 8], fromIndex = 2, endIndex = 4 Output [1, 2, 5, 6, 7, 8] Input list = ['G', 'E', 'E', 'G', 'G', 'K', 'S'], fromIndex = 3, endIndex = 5 Output ['G', 'E', 'E', 'K', 'S'] ... // Java code to remove a subList using // subList(a, b).clear() method import java.util.*; public class AbstractListDemo { public static void main(String args[]) { // Creating an empty AbstractList AbstractList<String> list = new LinkedList<String>(); // Using add() method // to add elements in the list list.add("GFG"); list.add("for"); list.add("Geeks"); list.add("computer"); list.add("portal"); // Output the list System.out.println("Original List: " + list); // subList and clear method // to remove elements // specified in the range list.subList(1, 3).clear(); // Print the final list System.out.println("Final List: " + list); } }
Find elsewhere
๐ŸŒ
Reddit
reddit.com โ€บ r/java โ€บ list.remove()
r/java on Reddit: List.remove()
October 23, 2025 -

I recently discovered that Java List (linked and array lists) in remove() method doesn't necessarily remove the exact given object (doesn't compare references using "==") but removes the first found object that is the same as the given one (compare using equals()). Can you somehow force it to remove the exact given object? It is problematic for handling a list possibly containing multiple different objects that have the same internal values.

๐ŸŒ
CodeGym
codegym.cc โ€บ java blog โ€บ java collections โ€บ arraylist removeall() method in java
ArrayList removeAll() method in Java
January 7, 2025 - At the beginning of this article I wrote that to remove all its values from the list, you need to use the ArrayList clear() method. It is, but of course you can also use the removeAll() method for this.
๐ŸŒ
Codecademy
codecademy.com โ€บ docs โ€บ java โ€บ arraylist โ€บ .remove()
Java | ArrayList | .remove() | Codecademy
March 21, 2022 - The .remove() method is used for removing specified elements from instances of the ArrayList class. ... Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more! ... Learn to code in Java โ€” a robust programming language ...
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ how-to-remove-an-element-from-a-java-list
How to remove an element from a Java List?
May 26, 2025 - In this example, we use the remove(object) method to remove the first occurrence of the specified element "Python" from the list {"Java", "JavaScript", "Python", "TypeScript"}:
๐ŸŒ
Learnerslesson
learnerslesson.com โ€บ JAVA โ€บ Java-Remove-from-List.htm
Java - Remove from List
Next, we have used the removeAll() method that removes all the elements from the ArrayList making the List empty.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ java โ€บ util โ€บ arraylist_remove.htm
Java ArrayList remove() Method
Following is the declaration for java.util.ArrayList.remove() method ... This method returns the element that was removed from the list .
๐ŸŒ
Java67
java67.com โ€บ 2014 โ€บ 03 โ€บ 2-ways-to-remove-elementsobjects-from-ArrayList-java.html
2 Ways to Remove Elements/Objects From ArrayList in Java [Example] | Java67
... There are two ways to remove objects from ArrayList in Java, first, by using the remove() method, and second by using Iterator. ArrayList provides overloaded remove() method, one accepts the index of the object to be removed i.e.
๐ŸŒ
W3Schools
w3schools.com โ€บ java โ€บ ref_arraylist_removeall.asp
Java ArrayList removeAll() Method
Remove multiple items from a list: import java.util.ArrayList; public class Main { public static void main(String[] args) { ArrayList<String> cars = new ArrayList<String>(); cars.add("Volvo"); cars.add("BMW"); cars.add("Ford"); cars.add("Mazda"); cars.add("Toyota"); ArrayList<String> remove = new ArrayList<String>(); remove.add("Volvo"); remove.add("Ford"); remove.add("Mazda"); cars.removeAll(remove); System.out.println(cars); } } Try it Yourself ยป ยท
๐ŸŒ
Mkyong
mkyong.com โ€บ home โ€บ java โ€บ java โ€“ how to remove items from a list while iterating?
Java - How to remove items from a List while iterating? - Mkyong.com
May 12, 2021 - Another removeIf example. ... package com.mkyong.basic; import java.util.ArrayList; import java.util.List; public class IteratorApp2B { public static void main(String[] args) { List<Integer> list = new ArrayList<>(); list.add(1); list.add(2); list.add(3); // remove if item is 1 or 3 list.removeIf(x -> x == 1 || x == 3); System.out.println(list); } }
๐ŸŒ
Reddit
reddit.com โ€บ r/processing โ€บ question and solution: how to delete or add an object to/from array list while iterating it.
r/processing on Reddit: Question and solution: How to delete or add an object to/from array list while iterating it.
December 28, 2022 - Another option is to create a new ArrayList containing all the elements you want to remove, and after the loop completes call mylist.removeAll(toRemove) (or, if you're discarding most of the list, reverse that and use mylist.retainAll(toKeep). Same basic logic either way, just different approaches. ... It depends on the specific sizes of the operations and how Java optimizes the execution.
๐ŸŒ
Oracle
docs.oracle.com โ€บ javase โ€บ 8 โ€บ docs โ€บ api โ€บ java โ€บ util โ€บ List.html
List (Java Platform SE 8 )
April 21, 2026 - IllegalArgumentException - if some property of an element of the specified collection prevents it from being added to this list ยท IndexOutOfBoundsException - if the index is out of range (index < 0 || index > size()) ... Removes from this list all of its elements that are contained in the specified collection (optional operation).
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ list-removeint-index-method-in-java-with-examples
List remove(int index) method in Java with Examples - GeeksforGeeks
November 29, 2024 - // Program to illustrate the // remove(int index) method import java.util.*; public class GFG { public static void main(String[] args) { // Declare an empty List of size 5 List<Integer> l = new ArrayList<Integer>(5); // Add elements to the list l.add(5); l.add(10); l.add(15); l.add(20); l.add(25); // Index from which you want to remove element int i = 2; // Initial list System.out.println("Initial List: " + l); // remove element l.remove(i); // Final list System.out.println("Final List: " + l); } }