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
Java Examples Java Videos Java Compiler Java Exercises Java Quiz Java Code Challenges Java Practice Problems Java Server Java Syllabus Java Study Plan Java Interview Q&A · ❮ ArrayList Methods · Remove 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.remove(0); System.out.println(cars); } } Try it Yourself » ·
🌐
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
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
How does an ArrayList remove an element from an array?
Please ensure that: Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions You include any and all error messages in full You ask clear questions You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions. Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar If any of the above points is not met, your post can and will be removed without further warning. Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png ) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc. Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit. Code blocks look like this: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above. If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures. To potential helpers Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns. More on reddit.com
🌐 r/javahelp
6
10
March 15, 2023
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
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › List.html
List (Java Platform SE 8 )
July 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 - The remove(int index) method of List interface in Java is used to remove an element from the specified index from a List container and returns the element after removing it.
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...

🌐
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.

Find elsewhere
🌐
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.
🌐
GeeksforGeeks
geeksforgeeks.org › java › list-removeobject-obj-method-in-java-with-examples
List remove(Object obj) method in Java with Examples - GeeksforGeeks
November 29, 2024 - The remove(Object obj) method of List interface in Java is used to remove the first occurrence of the specified element obj from this List if it is present in the List.
🌐
TutorialsPoint
tutorialspoint.com › java › util › arraylist_remove.htm
Java ArrayList remove() Method
Array size is printed, array is printed and using remove(index) method, an element is removed. Then size and array is printed again. package com.tutorialspoint; import java.util.ArrayList; public class ArrayListDemo { public static void ...
🌐
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 ...
🌐
Vultr
docs.vultr.com › java › standard library › java › util › arraylist › removeall()
Java ArrayList removeAll() - Remove Specified Elements
September 27, 2024 - The removeAll() method in Java's ArrayList class is a powerful tool for removing multiple elements from a list simultaneously based on the contents of another collection.
🌐
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) { ...
🌐
Scaler
scaler.com › home › topics › remove() in java
remove() in Java - Scaler Topics
April 7, 2024 - The remove method is often used in the Java Collection framework. The remove method removes the specified element from any collection of objects. However, the ways to remove an object might differ in one case or the other. The remove() method in ArrayList allows you to remove an element in two different ways. To begin with, you are supposed to know the object itself to get it removed from the list...
🌐
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 - No it won't. The ArrayList is still in the same order, minus the removed entries. JavaRanch-FAQ HowToAskQuestionsOnJavaRanch UseCodeTags DontWriteLongLines ItDoesntWorkIsUseLess FormatCode JavaIndenter SSCCE API-17 JLS JavaLanguageSpecification MainIsAPain KeyboardUtility ... Tan Quang wrote:Using get(index) with List is a bad thing, but it brings better performance with ArrayList.
🌐
TutorialsPoint
tutorialspoint.com › java › util › arraylist_remove_object.htm
Java.util.ArrayList.remove(Object) Method
The following example shows the usage of java.util.ArrayList.remove(object) method. package com.tutorialspoint; import java.util.ArrayList; public class ArrayListDemo { public static void main(String[] args) { // create an empty array list with an initial capacity ArrayList<String> arrlist = new ArrayList<String>(5); // use add() method to add values in the list arrlist.add("G"); arrlist.add("E"); arrlist.add("F"); arrlist.add("M"); arrlist.add("E"); System.out.println("Size of list: " + arrlist.size()); // let us print all the values available in list for (String value : arrlist) { System.out.println("Value = " + value); } // Removes first occurrence of "E" arrlist.remove("E"); System.out.println("Now, Size of list: " + arrlist.size()); // let us print all the values available in list for (String value : arrlist) { System.out.println("Value = " + value); } } }
🌐
Baeldung
baeldung.com › home › java › java list › removing an element from an arraylist
Removing an Element From an ArrayList | Baeldung
April 4, 2025 - 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.
🌐
GeeksforGeeks
geeksforgeeks.org › java › arraylist-removeall-method-in-java-with-examples
ArrayList removeAll() Method in Java with Examples - GeeksforGeeks
July 23, 2026 - Exception: This method throws NullPointerException if the list contains a null element and the specified collection does not permit null elements (optional), or if the specified collection is null. Example: Removing Specific Elements Using Another Collection ... import java.util.ArrayList; public class GFG { public static void main(String[] args) { // Creating an ArrayList of numbers ArrayList<Integer> n1 = new ArrayList<>(); n1.add(1); n1.add(2); n1.add(3); n1.add(4); n1.add(5); System.out.println("Original list: " + n1); // Creating another ArrayList // with elements to remove ArrayList<Integer> n2 = new ArrayList<>(); n2.add(1); n2.add(2); n2.add(3); // Removing specified elements // using removeAll() n1.removeAll(n2); System.out.println("List after removing specific elements: " + n1); } }
🌐
GeeksforGeeks
geeksforgeeks.org › java › remove-element-arraylist-java
How to remove an element from ArrayList in Java? - GeeksforGeeks
July 23, 2025 - // Java program to Remove Elements from ArrayList // Using remove() method by indices // Importing required classes import java.util.ArrayList; import java.util.List; // Main class public class GFG { // Main driver method public static void ...