Fork me on GitHub

【Java集合】Vector源码解析

Vector

1
2
3
public class Vector<E>
extends AbstractList<E>
implements List<E>, RandomAccess, Cloneable, java.io.Serializable

数据结构

  • Vector 是矢量队列,它是JDK1.0版本添加的类。继承于AbstractList,实现了List, RandomAccess, Cloneable这些接口。
  • 继承于AbstractList,是一个队列,支持相关的添加、删除、修改、遍历等功能
  • 实现了RandmoAccess接口,即提供了随机访问功能
  • 线程安全,不需要线程安全,则使用ArrayList
  • 初始长度10
  • 成员变量:
1
2
3
protected Object[] elementData;		//动态数组,增长由ensureCapacity()控制
protected int elementCount; //数组实际大小
protected int capacityIncrement; //动态数组的增长系数,创建时候若指定capacityIncrement,则每次扩容时候增加大小为capacityIncrement

源码分析

ensureCapacity方法

1
2
3
4
5
6
7
8
9
10
11
12
public synchronized void ensureCapacity(int minCapacity) {
if (minCapacity > 0) {
modCount++;
ensureCapacityHelper(minCapacity);
}
}

private void ensureCapacityHelper(int minCapacity) {
// overflow-conscious code
if (minCapacity - elementData.length > 0)
grow(minCapacity);
}

由grow()方法进行扩容操作

1
2
3
4
5
6
7
8
9
10
11
12
private void grow(int minCapacity) {
// overflow-conscious code
int oldCapacity = elementData.length;
//设置了capacityIncrement,则增长capacityIncrement长度,否则增长一倍
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); //复制到新数组中
}

删除元素

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public synchronized void removeElementAt(int index) {
modCount++;
if (index >= elementCount) {
throw new ArrayIndexOutOfBoundsException(index + " >= " +
elementCount);
}
else if (index < 0) {
throw new ArrayIndexOutOfBoundsException(index);
}
int j = elementCount - index - 1;
if (j > 0) {
System.arraycopy(elementData, index + 1, elementData, index, j);
}
elementCount--;
elementData[elementCount] = null; /* to let gc do its work */
}

增加元素

1
2
3
4
5
6
7
8
9
10
11
public synchronized void insertElementAt(E obj, int index) {
modCount++;
if (index > elementCount) {
throw new ArrayIndexOutOfBoundsException(index
+ " > " + elementCount);
}
ensureCapacityHelper(elementCount + 1);
System.arraycopy(elementData, index, elementData, index + 1, elementCount - index);
elementData[index] = obj;
elementCount++;
}

addAll()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public synchronized boolean addAll(int index, Collection<? extends E> c) {
modCount++;
if (index < 0 || index > elementCount)
throw new ArrayIndexOutOfBoundsException(index);

Object[] a = c.toArray();
int numNew = a.length;
ensureCapacityHelper(elementCount + numNew);

int numMoved = elementCount - index; //移动数据长度
if (numMoved > 0)
System.arraycopy(elementData, index, elementData, index + numNew,
numMoved);

System.arraycopy(a, 0, elementData, index, numNew);
elementCount += numNew;
return numNew != 0;
}

elements()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//返回Vector中全部元素对应的Enumeration, Enumeration迭代不会出现线程安全问题
public Enumeration<E> elements() {
return new Enumeration<E>() {
int count = 0;

public boolean hasMoreElements() {
return count < elementCount;
}

public E nextElement() {
synchronized (Vector.this) {
if (count < elementCount) {
return elementData(count++);
}
}
throw new NoSuchElementException("Vector Enumeration");
}
};
}

Stack

1
public class Stack<E> extends Vector<E>
  • Stack是栈。它的特性是:先进后出(FILO, First In Last Out)
  • 继承于Vector, 通过数组实现,而非链表

源码分析

push()

1
2
3
4
5
6
7
8
9
10
11
//将元素存入栈顶
public E push(E item) {
addElement(item); //在Vector中实现
return item;
}

public synchronized void addElement(E obj) {
modCount++;
ensureCapacityHelper(elementCount + 1);
elementData[elementCount++] = obj;
}

pop()

1
2
3
4
5
6
7
8
9
10
//获取栈顶元素,删除
public synchronized E pop() {
E obj;
int len = size();

obj = peek();
removeElementAt(len - 1);

return obj;
}

peek()

1
2
3
4
5
6
7
8
//获取栈顶元素,不删除
public synchronized E peek() {
int len = size();

if (len == 0)
throw new EmptyStackException();
return elementAt(len - 1);
}

总结  

  • Vector矢量队列,和ArrayList一样,它也是一个动态数组,由数组实现。但是ArrayList是非线程安全的,而Vector是线程安全的。   
  • Stack栈,它继承于Vector。它的特性是:先进后出(FILO, First In Last Out)