views:

4534

answers:

10

Hi guys,

I have a big doubt and I hope someone can help me out. 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:

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

2 - 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?

Thanks in advance,
Carlos

+6  A: 

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.

Mnementh
There is one caveat with `remove()` though: It may not remove the object you are currently looking at. Quoting from the JavaDoc: More formally, removes the element with the lowest index i such that (o==null ? get(i)==null : o.equals(get(i))) (if such an element exists)." So, depending on the objects in that list, it might not work as expected. Basically, if you could swap the ArrayList for a SortedSet without hurting your app, then you should be OK.
Hanno Fietz
This is what I see in javadoc for the remove() method of Iterator: "Removes from the underlying collection the last element returned by the iterator (optional operation). This method can be called only once per call to next. The behavior of an iterator is unspecified if the underlying collection is modified while the iteration is in progress in any way other than by calling this method." What am I missing?
Buhb
Thanks to all your answers, but I think Mnementh deserves the correct answer because he was the first who answered.
Carlos Pastor
This is worse than the two proposals in the question, because it leads to O(n²) runtime.
starblue
It also sucks because if an exception is thrown whilst iterating through the list then the list will be left in an inconsistent state. This could be an issue if the list is not local to the method.
pjp
pjp: That's also true of the `removeAll` approach, and most non-trivial mutations. starblue: O(n^2) is not necessarily slow. If the number of removals is relatively small it can fast.
Tom Hawtin - tackline
Yeah, ArrayList isn't the best solution, if you remove elements in the middle of it. But that is true for the removeAll()-approach too. Speaking about complexity, the new List should be working best. LinkedList also should work better on removes in the List. I add that to my answer.
Mnementh
A: 

Maybe Iterator’s remove() method? The JDK’s default collection classes should all creator iterators that support this method.

Bombe
+6  A: 

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.

RichardOD
Nice one, +1 :) Wonder if that performance improvement is actually noticeable.
Hanno Fietz
In some cases yes- probably one of those things to profile if you were concerned. Usually I take the strategy of building up another list of items to remove- it is easier for other programmers to understand.
RichardOD
Thanks, Richard, that's an interesting approach I've never thought of! +1
Carlos Pastor
Downvoters- please justify the downvote!
RichardOD
Yep, a great way to satisfy the requirements, and a common approach even in other languages. +1.
JPDecker
+3  A: 

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.

Buhb
+2  A: 

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();
    }
}
Jesper
+1  A: 

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();
}
gustafc
A: 

Unless you're positive that the issue you're facing is indeed a bottleneck, I would go for the readable

public ArrayList filterThings() {

    ArrayList pileOfThings;
    ArrayList filteredPileOfThings = new ArrayList();

    for (Thing thingy : pileOfThings) {
        if (thingy.property != 1) {
            filteredPileOfThings.add(thingy);
        }            
    }
    return filteredPileOfThings;
}
thomax
A: 

I'm good with Mnementh's recommentation.
Just one caveat though,

 ConcurrentModificationException

Mind that you don't have more than one thread running. This exception could appear if more than one thread executes, and the threads are not well synchronized.

Everyone
Not exactly - this will be thrown from a fail-fast collection if the underlying collection changes whilst iterating through it. For example you iterate over lst and one of your calls is to call remove on lst directly instead of using the remove method on ListIterator.
pjp
+1  A: 

There is a hidden cost in removing elements from an ArrayList. Each time you delete an element, you need to move the elements to fill the "hole". On average, this will take N / 2 assignments for a list with N elements.

So removing M elements from an N element ArrayList is O(M * N) on average. An O(N) solution involves creating a new list. For example.

List data = ...;
List newData = new ArrayList(data.size()); 

for (Iterator i = data.iterator(); i.hasNext(); ) {
    Object element = i.next();

    if ((...)) {
        newData.add(element);
    }
}

If N is large, my guess is that this approach will be faster than the remove approach for values of M as small as 3 or 4.

But it is important to create newList large enough to hold all elements in list to avoid copying the backing array when it is expanded.

Stephen C
A: 

I have found an alternative faster solution:

  int j = 0;
  for (Iterator i = list.listIterator(); i.hasNext(); ) {
    j++;

    if (campo.getNome().equals(key)) {
       i.remove();
       i = list.listIterator(j);
    }
  }