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

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

    The time complexity of ArrayList.clear() is O(n) and of removeAll is O(n^2).

    So yes, ArrayList.clear is much faster.

提交回复
热议问题