What is the difference between ArrayList.clear() and ArrayList.removeAll()?

前端 未结 9 1866
不思量自难忘°
不思量自难忘° 2020-11-30 16:05

Assuming that arraylist is defined as ArrayList arraylist, is arraylist.removeAll(arraylist) equivalent to arraylist.clear()?

9条回答
  •  感动是毒
    2020-11-30 16:54

    clear() will go through the underlying Array and set each entry to null;

    removeAll(collection) will go through the ArrayList checking for collection and remove(Object) it if it exists.

    I would imagine that clear() is way faster then removeAll because it's not comparing, etc.

提交回复
热议问题