3

So, sort of in continuation of this post: What is the difference between ArrayList.clear() and ArrayList.removeAll()?... Are there certain situations where it is actually better to use removeAll() instead of clear()?


Also, to add onto this question, if I know I'm clearing all the contents of an ArrayList, would it be ok to set it to a new ArrayList?

ArrayList myList = new ArrayList<String>();

myList.add("a");
myList.add("b");

// instead of using:  myList.clear();
myList = new ArrayList<String>()

If the above is ok to do, again, why use clear() vs setting to a new ArrayList? Creating a new, empty ArrayList is faster than O(n).

Community
  • 1
  • 1
philip yoo
  • 2,462
  • 5
  • 22
  • 37
  • 3
    Yes, if you need to `remove` well, `all` the elements in _another_ `List` from the current one. `clear` and `removeAll` are completely different methods and have completely different use cases. You almost **never** want to assign a `new ArrayList` rather than call `clear` - for example, how do you even know that the `List` you have is an `ArrayList`? – Boris the Spider Apr 01 '16 at 23:46

3 Answers3

10

Why use clear() instead of creating a new ArrayList? Several reasons:

  • You might not be allowed to reassign a reference field that points to an ArrayList, so you can clear an existing list but not put a new one in its place. For example:

    class MyData {
        // Can clear() but not reassign
        final List<Object> list = new ArrayList<>();
    }
    
  • The variable might be declared as List. The actual type could be LinkedList and you want to preserve it instead of replacing the implementation with an ArrayList.

  • It's not necessarily true that clear() is O(n) time. One implementation strategy is to nullify all the existing elements in the backing array in O(n) time. But another equally valid implementation is to throw away that internal array and replace it with a new one, preferably a short array for O(1) time.

Nayuki
  • 17,911
  • 6
  • 53
  • 80
1

There is no ArrayList.removeAll(). But there is an ArrayList.removeAll(Collection).

The method clear() should be faster than removeAll(Collection) because removeAll(Collection) does some comparisions to decide if an object should be removed from the list. The clear() method simply removes everything without thinking.

Nayuki
  • 17,911
  • 6
  • 53
  • 80
Shear Plane
  • 124
  • 8
  • These methods are completely _different_, and the question you are answering is already answered in the [linked question](https://stackoverflow.com/questions/7032070/what-is-the-difference-between-arraylist-clear-and-arraylist-removeall). You are [comparing apples and oranges](https://en.wikipedia.org/wiki/Apples_and_oranges) and not answering the question. – Boris the Spider Apr 01 '16 at 23:52
0

if you want to remove a specific collection from your list but you don't want to remove everything, you will use remove all with the collection that you want to remove.

Moshe9362
  • 352
  • 2
  • 15