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); } }
🌐
DigitalOcean
digitalocean.com › community › tutorials › java-list-remove-methods-arraylist-remove
How To Use remove() Methods for Java List and ArrayList | DigitalOcean
Learn how to use the remove() method in Java’s List and ArrayList interfaces with examples for removing by index or object.
Discussions

Java how to remove element from List efficiently - Stack Overflow
In fact, if you code oriented to ... 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 efficien... More on stackoverflow.com
🌐 stackoverflow.com
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. Share ... See also the retainAll and removeAll methods in the JavaDoc. Another option is to create a new ArrayList containing all the elements you want to ... More on reddit.com
🌐 r/processing
7
9
December 28, 2022
Removing an item from an array list
The problem is in this part: for (Student i : roster) { int sId = i.getId(); if (id == sId) { roster.remove(id); } else if (id != sId) { System.out.println("ERROR: Invalid student ID.\n"); } } You can't remove items from the list while you're looping through it with this for loop. If you want to use a loop, you should use this kind of loop: for (int i = 0; i < roster.size(); ++i) { Student s = roster.get(i); int sId = s.getId(); if (id == sId) { roster.remove(i); i--; // This is important! } } See, this lets you remove the student at the current index. However, as you remove it, you have to decrement the index, otherwise you'll skip the next item in the list. More on reddit.com
🌐 r/learnjava
8
5
March 31, 2017
🌐
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.
🌐
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.
🌐
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; ...
🌐
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...

Find elsewhere
🌐
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 ...
🌐
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!
🌐
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); } }
🌐
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.
🌐
Stack Overflow
stackoverflow.com › questions › 17279519 › removing-items-from-a-list
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) { ...
🌐
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.
🌐
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 - By iterating with a classic for loop from backwards, adding or removing an object 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. Share ... See also the retainAll and removeAll methods in the JavaDoc. 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).
🌐
BeginnersBook
beginnersbook.com › 2013 › 12 › java-arraylist-remove-method-example
Java ArrayList remove(int index) Method example
September 11, 2022 - Method remove(int index) is used for removing an element of the specified index from a list. It removes an element and returns the same. It throws IndexOutOfBoundsException if the specified index is less than zero or greater than the size of the list (index size of ArrayList).
🌐
Cscode
cscode.io › java › collections › delete list element while iterating
How to delete elements while iterating from List in Java | CsCode.io
public void deleteWhileIteratingListUsingIterator() { List<String> arrayList = new ArrayList<>(List.of("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k")); System.out.println("BEFORE - arrayList size : "+ arrayList.size() + " , elements : "+arrayList); Iterator<String> ite = arrayList.iterator(); while(ite.hasNext()) { String element = ite.next(); if("c".equals(element)) { ite.remove(); // removes current element from iterator position } } System.out.println("AFTER - arrayList size : "+ arrayList.size() + " , elements : "+arrayList); }
🌐
TutorialsPoint
tutorialspoint.com › how-to-remove-an-element-from-a-java-list
Remove elements from a linked list
May 26, 2025 - You can use this method to remove elements from a linked list. import java.util.LinkedList; public class RemovingElements { public static void main(String[] args) { LinkedList linkedList = new LinkedList(); linkedList.add("Mangoes"); linkedList.add("Grapes"); linkedList.add("Bananas"); linkedList.add("Oranges"); linkedList.add("Pineapples"); System.out.println("Contents of the linked list :"+linkedList); linkedList.remove("Grapes"); System.out.println("Contents of the linked list after removing the specified element :"+linkedList); } }