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

前端 未结 9 1881
不思量自难忘°
不思量自难忘° 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 17:04

    Unless there is a specific optimization that checks if the argument passed to removeAll() is the collection itself (and I highly doubt that such an optimization is there) it will be significantly slower than a simple .clear().

    Apart from that (and at least equally important): arraylist.removeAll(arraylist) is just obtuse, confusing code. It is a very backwards way of saying "clear this collection". What advantage would it have over the very understandable arraylist.clear()?

提交回复
热议问题