这三者都是实现集合框架的List,也就是所谓的有序集合,因此具体功能都实现了按照位置进行定位,修改,添加或者删除,都提供了迭代器以遍历其内容,但因为具体设计不同,在行为,性能,线程安全等方面,表现又有很大不同。
Vector是java早期提供的线程安全的动态数组,但数据满时,自动扩容,会创建新的数组,并拷贝原有的数组。
ArrayList是应用更广泛的动态数组,它本身是线程不安全的,所以性能会好很多,扩容时,和vector不一样,vector是扩大一倍,而ArrayList是增加50%。
LinkedList是java提供的双向链表,它不是线程安全的,也需要向上面两种调整容量。
知识扩展
1.Vector和ArrayList作为动态数组,它内部是数组实现的,十分便于随机访问,除了尾部插入数据,或者删除数据时,性能是相对较差的。比如我们在中部插入数据,需要后移所有数据。
2.LinkedList进行结点插入删除很高效,但是随机访问比动态数据慢很多。
源码
Vector
//Vector
protected Object[] elementData;
//动态数组
public Vector(int initialCapacity) {
this(initialCapacity, 0);
}
/**
* Constructs an empty vector so that its internal data array
* has size {@code 10} and its standard capacity increment is
* zero.
*/
public Vector() {
this(10);
}
//初始化动态数组默认大小为10
public synchronized boolean add(E e) {
modCount++;
ensureCapacityHelper(elementCount + 1);
elementData[elementCount++] = e;
return true;
}
private void ensureCapacityHelper(int minCapacity) {
// overflow-conscious code
if (minCapacity - elementData.length > 0)
grow(minCapacity);
}
private void grow(int minCapacity) {
// overflow-conscious code
int oldCapacity = elementData.length;
int newCapacity = oldCapacity + ((capacityIncrement > 0) ?
capacityIncrement : oldCapacity);
if (newCapacity - minCapacity < 0)
newCapacity = minCapacity;
if (newCapacity - MAX_ARRAY_SIZE > 0)
newCapacity = hugeCapacity(minCapacity);
elementData = Arrays.copyOf(elementData, newCapacity);
}
//这边可以看出,扩容时,是扩大一倍。 int newCapacity = oldCapacity + ((capacityIncrement > 0) ?capacityIncrement : oldCapacity);
public synchronized int indexOf(Object o, int index) {
if (o == null) {
for (int i = index ; i < elementCount ; i++)
if (elementData[i]==null)
return i;
} else {
for (int i = index ; i < elementCount ; i++)
if (o.equals(elementData[i]))
return i;
}
return -1;
}
//定位时会对object进行判断,如果是null,用==判断,如果不是null,用equals
public synchronized Iterator<E> iterator() {
return new Itr();
}
/**
* An optimized version of AbstractList.Itr
*/
private class Itr implements Iterator<E> {
int cursor; // index of next element to return
int lastRet = -1; // index of last element returned; -1 if no such
int expectedModCount = modCount;
public boolean hasNext() {
// Racy but within spec, since modifications are checked
// within or after synchronization in next/previous
return cursor != elementCount;
}
public E next() {
synchronized (Vector.this) {
checkForComodification();
int i = cursor;
if (i >= elementCount)
throw new NoSuchElementException();
cursor = i + 1;
return elementData(lastRet = i);
}
}
}
//对vector进行遍历也就方便了
Iterator<String> it = vector.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
ArrayList
transient Object[] elementData;
//动态数组
private void grow(int minCapacity) {
// overflow-conscious code
int oldCapacity = elementData.length;
int newCapacity = oldCapacity + (oldCapacity >> 1);
if (newCapacity - minCapacity < 0)
newCapacity = minCapacity;
if (newCapacity - MAX_ARRAY_SIZE > 0)
newCapacity = hugeCapacity(minCapacity);
// minCapacity is usually close to size, so this is a win:
elementData = Arrays.copyOf(elementData, newCapacity);
}
//扩容时,是>>1,也就是/2。所以,就是扩容50%。
public boolean add(E e) {
ensureCapacityInternal(size + 1); // Increments modCount!!
elementData[size++] = e;
return true;
}
//线程不安全
LinkedList
transient int size = 0;
/**
* Pointer to first node.
* Invariant: (first == null && last == null) ||
* (first.prev == null && first.item != null)
*/
transient Node<E> first;
/**
* Pointer to last node.
* Invariant: (first == null && last == null) ||
* (last.next == null && last.item != null)
*/
transient Node<E> last;
//两个链表
public boolean add(E e) {
linkLast(e);
return true;
}
void linkLast(E e) {
final Node<E> l = last;
final Node<E> newNode = new Node<>(l, e, null);
last = newNode;
if (l == null)
first = newNode;
else
l.next = newNode;
size++;
modCount++;
}
//当然还有pop,push方法,linkedList不仅仅继承了AbstractList而且继承了Deque,可以实现双向队列使用