首先,推荐大家使用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;
}大家看出差异了吗?
