for (Iterator<String> iter = list.listIterator(); iter.hasNext(); ) {
    String a = iter.next();
    if (...) {
        iter.remove();
    }
}

Making an additional assumption that the list is of strings. As already answered, an list.iterator() is needed. The listIterator can do a bit of navigation too.

–---------

Update

As @AyushiJain commented, there is

list.removeIf(...);
Answer from Joop Eggen 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); } }
Discussions

Question and solution: How to delete or add an object to/from array list while iterating it.
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). Same basic logic either way, just different approaches. More on reddit.com
🌐 r/processing
7
9
December 28, 2022
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) Can't remove element from List/ArrayList, always gives NullPointerException or ConcurrentModificationException.
You add every line to the list, but the last line of the file will always return null. You correctly end the loop at that point, but you still add null to the list. That's what is giving those nullpointer exceptions More on reddit.com
🌐 r/AskProgramming
6
1
October 25, 2019
Nested iteration over same list, want to modify (remove) elements in the inner loop. Best practices?
The best approach is to do the nested iteration in your first example, but rather than remove from the original list, build a second list of "items to keep". You can then just stop referencing the original list and let garbage collection do the work. Keep in mind you're working with references in java; you won't be copying objects, but rather references. Not much performance concern in building new lists even when fairly large. More on reddit.com
🌐 r/javahelp
10
3
June 6, 2015
🌐
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 -

Hi few days ago i was encountered a problem. While iterating an array list by a for each loop, i was trying to remove the current object in spesific conditional. Like:

Arraylist<someclass> mylist;

...

for (someclass element: mylist)

if element.data!=0; mylist.remove(element)

when i run the code it throws ConcurrentModificationException .

I think it is because of the size of arraylist changes and the next iteration also changes and it throws out error.

Then I remembered the solition from a book.

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:

for (int i=mylist.size()-1;i>=0;i--)

someclass element=mylist.get(i);

if element.data!=0; mylist.remove(i)

with this method it works and there was no error.

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.

🌐
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 ...
🌐
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.
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
🌐
Quora
quora.com › How-do-you-remove-the-first-and-last-elements-from-a-Java-List
How to remove the first and last elements from a Java List - Quora
Answer (1 of 2): You’re not specifying the list type to be used. The most efficient way to remove the head and tail nodes of a list depends on the concrete list. The general solution (ignoring bound checks for now) is to do the following: [code]List list = ...; list.remove(0); list.remov...
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › List.html
List (Java Platform SE 8 )
October 20, 2025 - 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-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.
🌐
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; ...
🌐
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); } }
🌐
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); } }
🌐
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.
🌐
Coderanch
coderanch.com › t › 727795 › java › removing-element-List
Problem removing an element from a List (Beginning Java forum at Coderanch)
March 7, 2020 - I don't think Java likes it when you try to remove an entry from a List inside a For Each loop. It's considered concurrent modification of the List and is not allowed. I recommend that you try an Iterator on the list. Loop through using iterator.next and hasNext and then do remove when you find your desired entry to remove.
🌐
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.
🌐
LeetCode
leetcode.com › problems › remove-linked-list-elements
Remove Linked List Elements - LeetCode
Can you solve this real interview question? Remove Linked List Elements - Given the head of a linked list and an integer val, remove all the nodes of the linked list that has Node.val == val, and return the new head.
🌐
W3Schools
w3schools.com › java › java_arraylist.asp
Java ArrayList
While elements can be added and removed from an ArrayList whenever you want. To use an ArrayList, you must first import it from java.util: Create an ArrayList object called cars that will store strings: import java.util.ArrayList; // Import the ArrayList class ArrayList<String> cars = new ArrayList<String>(); // Create an ArrayList object · Now you can use methods like add(), get(), set(), and remove() to manage your list of elements.
🌐
LeetCode
leetcode.com › problems › remove-element
Remove Element - LeetCode
Remove Element - Given an integer array nums and an integer val, remove all occurrences of val in nums in-place [https://en.wikipedia.org/wiki/In-place_algorithm]. The order of the elements may be changed.