首先,推荐大家使用ArrayList,了解这个差别,更多是为了应对面试。

两者的最大差异就是线程安全

ArrayList:线程不安全,但性能高

Vector:线程安全,但性能较低

我们如何得到一个类是线程安全或不安全的结论的?

从源码的角度来说,你大可以打开ArrayList和Vector的源码一对比,即可发现

ArrayList的部分源码:

public boolean add(E e) {ensureCapacityInternal(size + 1); // Increments modCount!!elementData[size++] = e;return true;}

Vector的部分源码:

public synchronized boolean add(E e) {modCount++;ensureCapacityHelper(elementCount + 1);elementData[elementCount++] = e;return true;}

大家看出差异了吗?

每天一道面试题-ArrayList和Vector的区别

相关文章:

  • 2021-12-08
  • 2021-10-06
  • 2021-04-07
  • 2021-06-13
  • 2021-07-08
  • 2021-08-06
  • 2022-12-23
猜你喜欢
  • 2021-10-03
  • 2021-05-21
  • 2021-04-18
  • 2021-08-15
  • 2021-06-01
  • 2022-01-18
  • 2021-09-02
相关资源
相似解决方案