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 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 the removeAll()
method passing the ArrayList
with the objects to be deleted.
Is there a more efficient way to delete objects from an ArrayList
?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…