简介:
通过阅读源码发现Vector和ArrayList基本上是很相似的,只是Vector的增删改查方法上都加了synchronized关键字,所以是线程安全的,但是直接通过synchronized同步机制实现的线程安全会导致效率很低,所以基本上Vector很少被使用,下面就是Vector的增删改查方法和一起辅助方法的源码分析。本来是不打算看这个源码的,想想还是看一下吧,雨露均沾是不是:)
成员变量
/**
* Vector底层数组
*/
protected Object[] elementData;
/**
* 底层数组的元素个数
*/
protected int elementCount;
/**
* 扩容大小,即每次扩容增长的容量,默认扩容为原来容量的两倍。
*/
protected int capacityIncrement;
/** 序列号 */
private static final long serialVersionUID = -2767605614048989439L;
构造器
构造器的代码很简单,就没有写太多注释了。这里并没有使用lazy-load机制,和其他大部分集合框架1.8更新的还是有点区别的,可能是使用的场景太少了,已经没有进行过维护了:)
/**
* 自定义初始化容量,和扩容大小的构造器
*/
public Vector(int initialCapacity, int capacityIncrement) {
super();
if (initialCapacity < 0)
throw new IllegalArgumentException("Illegal Capacity: "+
initialCapacity);
this.elementData = new Object[initialCapacity];
this.capacityIncrement = capacityIncrement;
}
/**
* 自定义初始化容量的构造器
*/
public Vector(int initialCapacity) {
this(initialCapacity, 0);
}
/**
* 默认构造器,默认初始化容量为 10,扩容标志为 0
*/
public Vector() {
this(10);
}
/**
* 自定义集合的构造器
*/
public Vector(Collection<? extends E> c) {
// 集合转成数组
elementData = c.toArray();
// 元素数就是指定集合中的元素素
elementCount = elementData.length;
// 不是Object[],则转成Object[]
if (elementData.getClass() != Object[].class)
elementData = Arrays.copyOf(elementData, elementCount, Object[].class);
}
add方法
add方法无非是指定元素并指定索引,或者指定元素不指定索引。
/**
* 添加元素,从末尾添加(线程安全的)
*/
public synchronized boolean add(E e) {
// 操作记录数加1
modCount++;
// 确认是否进行扩容
ensureCapacityHelper(elementCount + 1);
// 在末尾添加新的元素
elementData[elementCount++] = e;
return true;
}
/**
* 指定位置插入指定元素
*/
public void add(int index, E element) {
insertElementAt(element, index);
}
/**
* 指定位置插入指定元素的具体方法
*/
public synchronized void insertElementAt(E obj, int index) {
// 操作记录数加1
modCount++;
if (index > elementCount) {
throw new ArrayIndexOutOfBoundsException(index
+ " > " + elementCount);
}
// 确认是否进行扩容
ensureCapacityHelper(elementCount + 1);
// 插入位置之后的元素进行索引重排
System.arraycopy(elementData, index, elementData, index + 1, elementCount - index);
// 将新元素赋值给插入位置
elementData[index] = obj;
// 元素个数加1
elementCount++;
}
@SuppressWarnings("unchecked")
E elementData(int index) {
return (E) elementData[index];
}
指定位置插入指定元素方法的帮助理解图:
增加方法必然会设计到扩容的方法,下面是源码
/**
* 确认扩容
*/
public synchronized void ensureCapacity(int minCapacity) {
if (minCapacity > 0) {
modCount++;
ensureCapacityHelper(minCapacity);
}
}
/**
* 确认扩容的方法,这个方法不是一个线程安全的方法,所以其他线程安全的方法在调用这个方法时
* ,不会产生额外的开销(加锁,释放锁)。
*/
private void ensureCapacityHelper(int minCapacity) {
// 最小的元素个数比底层数组容量大时进行扩容
if (minCapacity - elementData.length > 0)
// 调用具体的扩容方法
grow(minCapacity);
}
具体的grow方法
/**
* 我感觉这个是即将越界前的此次扩容缓冲值,防止扩容时发生越界
*/
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
private void grow(int minCapacity) {
// 记录扩容前数组的容量(长度)
int oldCapacity = elementData.length;
// 计算扩容后的数组容量,再没有有指定扩容大小的时候,默认扩容为原谅容量的两倍
int newCapacity = oldCapacity + ((capacityIncrement > 0) ?
capacityIncrement : oldCapacity);
if (newCapacity - minCapacity < 0)
// 这个操作我想可能是为了防止自定义的 capacityIncrement 为负数吧。
newCapacity = minCapacity;
if (newCapacity - MAX_ARRAY_SIZE > 0)
// 调用判断容量是否符合int类型的范围的方法
newCapacity = hugeCapacity(minCapacity);
// 创建扩容后的数组,并将原来数组中的元素copy到新的数组中,再设置为Vector的底层数组
elementData = Arrays.copyOf(elementData, newCapacity);
}
/**
* 判断容量是否符合int类型的范围的方法
*/
private static int hugeCapacity(int minCapacity) {
if (minCapacity < 0) // overflow
throw new OutOfMemoryError();
return (minCapacity > MAX_ARRAY_SIZE) ?
Integer.MAX_VALUE :
MAX_ARRAY_SIZE;
}
还有两个插入指定集合中所有元素的方法,也是分为指定索引和不指定。
/**
* 在末尾添加指定集合中的所有元素
*/
public synchronized boolean addAll(Collection<? extends E> c) {
// 操作记录数加1
modCount++;
// 集合转成数组
Object[] a = c.toArray();
// 记录指定集合中元素个数
int numNew = a.length;
// 扩容检查
ensureCapacityHelper(elementCount + numNew);
// 末尾插入元素
System.arraycopy(a, 0, elementData, elementCount, numNew);
// 设置元素个数
elementCount += numNew;
return numNew != 0;
}
/**
* 在指定位置添加指定集合中的所有元素到当前Vector中
*/
public synchronized boolean addAll(int index, Collection<? extends E> c) {
// 操作记录数加1
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;
}
remove方法
删除方法分为:删除指定元素和删除指定索引位置的元素。
/**
* 删除指定索引位置的元素,并返回删除的元素
*/
public synchronized E remove(int index) {
// 操作记录数加1
modCount++;
if (index >= elementCount)
throw new ArrayIndexOutOfBoundsException(index);
// 记录需要删除的元素
E oldValue = elementData(index);
// 记录删除元素的索引位置
int numMoved = elementCount - index - 1;
if (numMoved > 0)
// 重排数组
System.arraycopy(elementData, index+1, elementData, index,
numMoved);
// 将最后一个元素赋值为null
elementData[--elementCount] = null; // Let gc do its work
// 返回删除的元素
return oldValue;
}
/**
* 删除指定元素,本质还是根据索引删除,先去找到指定元素对应的索引,再根据索引删除
*/
public boolean remove(Object o) {
// 调用删除元素方法
return removeElement(o);
}
/**
* 删除指定元素
*/
public synchronized boolean removeElement(Object obj) {
// 操作记录数加1
modCount++;
// 寻找指定元素的索引
int i = indexOf(obj);
if (i >= 0) {
// 本质上还是通过索引来删除
removeElementAt(i);
return true;
}
return false;
}
/**
* 寻找指定元素的索引
*/
public int indexOf(Object o) {
return indexOf(o, 0);
}
/**
* 寻找指定元素的索引,这个方法也是用来查询指定索引之后是否包含指定元素的方法。
*/
public synchronized int indexOf(Object o, int index) {
// 遍历寻找指定元素,判断null,应为null不能调用equals方法
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;
}
// 没有这个元素,返回-1
return -1;
}
/**
* 删除指定索引的元素,基本上和remove(int) 方法一样,只是没有返回值,这里就没写注释了
*/
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 */
}
删除指定索引的元素方法的帮助理解图:
下面还有一些批量删除的方法
/**
* 删除在 fromIndex 到 toIndex 之间的元素
*/
protected synchronized void removeRange(int fromIndex, int toIndex) {
// 操作记录数加1
modCount++;
// 记录删除元素的个数
int numMoved = elementCount - toIndex;
// 进行数组重排
System.arraycopy(elementData, toIndex, elementData, fromIndex,
numMoved);
// 计算删除后的元素个数
int newElementCount = elementCount - (toIndex-fromIndex);
// 遍历将 toIndex 之后的元素赋值为null
while (elementCount != newElementCount)
elementData[--elementCount] = null;
}
/**
* 删除所有元素
*/
public void clear() {
removeAllElements();
}
/**
* 删除所有元素,简单的将所有元素赋值为null,并将元素数设置为0
*/
public synchronized void removeAllElements() {
// 操作记录数加1
modCount++;
// 遍历将所有元素赋值为null
for (int i = 0; i < elementCount; i++)
elementData[i] = null;
// 元素数赋值为0
elementCount = 0;
}
get方法
get方法就更简单了
/**
* 获取指定索引的元素值
*/
public synchronized E get(int index) {
if (index >= elementCount)
throw new ArrayIndexOutOfBoundsException(index);
return elementData(index);
}
/**
* 获取第一个元素
*/
public synchronized E firstElement() {
if (elementCount == 0) {
throw new NoSuchElementException();
}
return elementData(0);
}
/**
* 获取最后一个元素
*/
public synchronized E lastElement() {
if (elementCount == 0) {
throw new NoSuchElementException();
}
return elementData(elementCount - 1);
}
set方法
/**
* 修改指定索引位置的值为指定值,并返回修改之前的值
*/
public synchronized E set(int index, E element) {
if (index >= elementCount)
throw new ArrayIndexOutOfBoundsException(index);
// 记录修改之前的元素值
E oldValue = elementData(index);
// 将指定值赋值给指定索引位置
elementData[index] = element;
// 返回修改之前的值
return oldValue;
}
/**
* 删除指定索引位置的元素,没有返回值,逻辑和set(E,int)方法一样
*/
public synchronized void setElementAt(E obj, int index) {
if (index >= elementCount) {
throw new ArrayIndexOutOfBoundsException(index + " >= " +
elementCount);
}
elementData[index] = obj;
}
其他方法
/**
* 线程安全的copy方法,将Vector底层数组中的所有元素copy到指定数组上
*/
public synchronized void copyInto(Object[] anArray) {
System.arraycopy(elementData, 0, anArray, 0, elementCount);
}
/**
* 获取容量
*/
public synchronized int capacity() {
return elementData.length;
}
/**
* 获取元素个数
*/
public synchronized int size() {
return elementCount;
}
/**
* 判断Vector是否为空
*/
public synchronized boolean isEmpty() {
return elementCount == 0;
}
/**
* 判断是否存在指定元素
*/
public boolean contains(Object o) {
return indexOf(o, 0) >= 0;
}
/**
* 是否全部包含指定集合中的元素
*/
public synchronized boolean containsAll(Collection<?> c) {
return super.containsAll(c);
}
这样Vector的源就看完了。