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
If a value is specified and multiple elements in the list have the same value then only the first one is deleted. If the list contains integers and you want to delete an integer based on its value you will need to pass an Integer object. See More Examples below for an example. ... T refers to the data type of items in the list. 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); } }
🌐
GeeksforGeeks
geeksforgeeks.org › java › remove-element-arraylist-java
How to remove an element from ArrayList in Java? - GeeksforGeeks
July 23, 2025 - This may lead to ConcurrentModificationException When iterating over elements, it is recommended to use Iterator.remove() method. ... // Java program to demonstrate working of // Iterator.remove() on an integer ArrayList import java.util.ArrayList; ...
🌐
Baeldung
baeldung.com › home › java › java list › removing an element from an arraylist
Removing an Element From an ArrayList | Baeldung
April 4, 2025 - 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.
🌐
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...

🌐
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!
Find elsewhere
🌐
TutorialsPoint
tutorialspoint.com › article › how-do-i-remove-multiple-elements-from-a-list-in-java
How do I remove multiple elements from a list in Java?
June 10, 2025 - Following is an example of removing multiple elements from a list using a for loop: import java.util.ArrayList; import java.util.List; public class RemoveMultipleElements { public static void main(String[] args) { List<Integer> list = new ArrayList<>(); list.add(1); list.add(2); list.add(3); list.add(4); list.add(5); System.out.println("Original List: " + list); List<Integer> elementsToRemove = new ArrayList<>(); elementsToRemove.add(2); elementsToRemove.add(4); for (int i = 0; i < list.size(); i++) { if (elementsToRemove.contains(list.get(i))) { list.remove(i); i--; } } System.out.println("List after removal: " + list); } }
🌐
Baeldung
baeldung.com › home › java › java collections › removing elements from java collections
Removing Elements from Java Collections | Baeldung
January 8, 2024 - Java’s Iterator enables us to both walk and remove every individual element within a Collection.
🌐
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); } }
🌐
Coderanch
coderanch.com › t › 750418 › java › Remove-element-ArrayList-List-loop
Remove the element of ArrayList and List in for-loop indexes? (Java in General forum at Coderanch)
March 22, 2022 - Mike Simmons wrote:Other than the unnecessary continue, there's nothing really wrong with your implementation using Iterator.remove(). I already showed code to do that, and the timing is just as bad as the forward loop, for the same fundamental reason. Each remove() is moving a bunch of elements by one index position. Lots of unnecessary work. For whatever reason you need to use Java 7... you can still use the method I labeled as Iterator on LinkedList, or make new list.
🌐
Javadeveloperzone
javadeveloperzone.com › java-basic › java-remove-elemen-from-list
Java Remove Element from List – Java Developer Zone
List<String> languages = new ArrayList<>(Arrays.asList("Java", "C", "PHP", "Net", "VB")); languages.removeIf(language -> language.equalsIgnoreCase("C")); // Its remove all element which satisfied predicate condition. languages.forEach(language -> { System.out.println(language); }); ... Retains only the elements in this list that are contained in the specified collection (optional operation). In other words, removes from this list all of its elements that are not contained in the specified collection.
🌐
Cscode
cscode.io › java › collections › delete elements from list
How to delete elements from List in Java | CsCode.io
In below examples we will reference ... methods like if it's an un modifiable list etc. ... List.remove(int i) method where you can pass the index of an element......
🌐
Programiz
programiz.com › java-programming › library › arraylist › remove
Java ArrayList remove()
It is because the remove() method only takes objects as its arguments. To learn more, visit Java Primitive Types to Wrapper Objects. remove() - Removes the element 13 that appeared first in the arraylist. Note: We can also remove all the elements from the arraylist using the clear() method.
🌐
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"}:
🌐
Vultr
docs.vultr.com › java › standard-library › java › util › ArrayList › remove
Java ArrayList remove() - Delete Element | Vultr Docs
November 14, 2024 - Create an ArrayList with some elements. Use the remove() method with the object you want to delete. ... ArrayList<String> pets = new ArrayList<>(); pets.add("Dog"); pets.add("Cat"); pets.add("Bird"); pets.remove("Cat"); System.out.println(pets); ...
🌐
Baeldung
baeldung.com › home › java › java list › remove all occurrences of a specific value from a list
Remove All Occurrences of a Specific Value from a List | Baeldung
June 27, 2025 - In Java, it’s straightforward to remove a specific value from a List using List.remove(). However, efficiently removing all occurrences of a value is much harder. In this tutorial, we’ll see multiple solutions to this problem, describing the pros and cons. For the sake of readability, we use a custom list(int…) method in the tests, which returns an ArrayList containing the elements ...