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 this ArrayList and delete all elements meeting a certain condition.
As far as I know, those would be my options to delete:
Create a new
ArrayListand 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
ArrayListand add the elements that meet the condition. After the iteration, use theremoveAll()method passing theArrayListwith the objects to be deleted.
Is there a more efficient way to delete objects from an ArrayList?