The difference between arraylist and vector

There are three construction methods for arraylist:

public ArrayList(int initialCapacity)//构造一个具有指定初始容量的空列表。
public ArrayList()//构造一个初始容量为 10 的空列表。
public ArrayList(Collection<? extends E> c)//构造一个包含指定 collection 的元素的列表

Vector has four construction methods:

public Vector()//使用指定的初始容量和等于零的容量增量构造一个空向量。
public Vector(int initialCapacity)//构造一个空向量,使其内部数据数组的大小,其标准容量增量为零。
public Vector(Collection<? extends E> c)//构造一个包含指定 collection 中 的元素的向量
public Vector(int initialCapacity,int capacityIncrement)//使用指定的初始容量和容量增量构造一个空的向量

Both ArrayList and Vector are implemented with arrays. There are four main differences:
(1) Vector is multi-thread safe. Thread safety means that multiple threads access the same code without generating uncertain results.
And ArrayList is not. This can be seen from the source code. Many methods in the Vector class are
modified



by synchronized , which results in Vector not being compared with ArrayList in efficiency; (2) Both use linear continuous space storage Element, but when the space is insufficient, the two categories increase in different ways . (3) Vector can set the growth factor, but not ArrayList. (4) Vector is an old dynamic array. It is thread-synchronized and has low efficiency. It is generally deprecated to use it.
Application scenario analysis:
1) Vector is thread-synchronous, so it is also thread-safe, while ArrayList is thread-asynchronous, which is unsafe.
If thread safety is not taken into consideration, ArrayList is generally more efficient.
2) If the number of elements in the collection is greater than the length of the current collection array, using a relatively large amount of data in the collection
, using Vector has certain advantages.

Guess you like

Origin blog.csdn.net/lthahaha/article/details/114236001