Deleting objects from an ArrayList in Java
Solution 1
Another way: The Iterator has an optional remove()-method, that is implemented for ArrayList. You can use it while iterating.
I don't know though, which variant is the most performant, you should measure it.
starblue commented, that the complexity isn't good, and that's true (for removeAll() too), because ArrayList has to copy all elements, if in the middle is an element added or removed. For that cases should a LinkedList work better. But, as we all don't know your real use-cases the best is too measure all variants, to pick the best solution.
Solution 2
You could iterate backwards and remove as you go through the ArrayList. This has the advantage of subsequent elements not needing to shift and is easier to program than moving forwards.
Solution 3
Most performant would, I guess, be using the listIterator
method and do a reverse iteration:
for (ListIterator<E> iter = list.listIterator(list.size()); iter.hasPrevious();){
if (weWantToDelete(iter.previous())) iter.remove();
}
Edit: Much later, one might also want to add the Java 8 way of removing elements from a list (or any collection!) using a lambda or method reference. An in-place filter
for collections, if you like:
list.removeIf(e -> e.isBad() && e.shouldGoAway());
This is probably the best way to clean up a collection. Since it uses internal iteration, the collection implementation could take shortcuts to make it as fast as possible (for ArrayList
s, it could minimize the amount of copying needed).
Solution 4
Obviously, of the two methods you mention number 1 is more efficient, since it only needs to go through the list once, while with method number 2 the list has to be traversed two times (first to find the elements to remove, and them to remove them).
Actually, removing a list of elements from another list is likely an algorithm that's worse than O(n) so method 2 is even worse.
The iterator method:
List data = ...;
for (Iterator i = data.iterator(); i.hasNext(); ) {
Object element = i.next();
if (!(...)) {
i.remove();
}
}
Solution 5
First, I'd make sure that this really is a performance bottleneck, otherwise I'd go with the solution that is cleanest and most expressive.
If it IS a performance bottleneck, just try the different strategies and see what's the quickest. My bet is on creating a new ArrayList and puting the desired objects in that one, discarding the old ArrayList.
Carlos Pastor
Updated on May 06, 2020Comments
-
Carlos Pastor about 4 years
I need to delete some objects from an
ArrayList
if they meet a condition and I'm wondering which way could be more efficient.Here's the situation: I have a class that contains an
ArrayList
containing some other objects. I have to iterate over thisArrayList
and delete all elements meeting a certain condition. As far as I know, those would be my options to delete:Create a new
ArrayList
and add the elements that doesn't meet the condition. After the iteration, swap from the old arraylist to the new one without the elements.Create a new
ArrayList
and add the elements that meet the condition. After the iteration, use theremoveAll()
method passing theArrayList
with the objects to be deleted.
Is there a more efficient way to delete objects from an
ArrayList
?