Vector 继承关系
Vector 是在 JDK 1.0 就提供的类,在 JDK 1.2 对 Vector 进行改造后,Vector 和 ArrayList 都继承实现了相同的类和接口。
public class Vector<E> extends AbstractList<E>
implements List<E>, RandomAccess, Cloneable, java.io.Serializable {
}
Vector 实现了 RandomAccess 接口说明 Vector 也支持随机访问,在 Vector 中,可以通过元素的索引快速获取元素对象,这就是快速随机访问。
类文档解读
老规矩,先通过 Vector 的类文档了解一下能得到哪些信息。
- Vector 内部是一个大小可增长的 Object 类型的数组,可以通过索引访问 Vector 内部的元素,这点和 ArrayList 一样。
- 和 ArrayList 一样 Vector 使用数组作为底层的数据结构,也涉及到扩容的问题,建议在添加大量数据前手动调用 ensureCapacity 方法进行扩容减少自动扩容的次数。
- Vector 的 iteraor 和 listIterator 方法返回的迭代器是 fail-fast 策略的。
- Vector 是线程安全的,如果不需要线程安全,建议使用 ArrayList 代替 Vector。
Vector API
成员变量
// 存储数据的数组,此数组的容量一般大于实际长度,后续未填充数据用 null 替代
protected Object[] elementData;
// Vector 的数据数量
protected int elementCount;
// 当 vector 容量不足以容纳元素时,自动扩容的大小。如果此值小于等于 0 则扩容一倍
protected int capacityIncrement;
// 最大容量
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
构造方法
// 构造一个空 Vector,使其内部数组的大小为 10,其标准容量增量为 0。
public Vector() {
this(10);
}
// 指定初始容量
public Vector(int initialCapacity) {
this(initialCapacity, 0);
}
public Vector(int initialCapacity, int capacityIncrement) {
// 调 AbstractList 的无参构造
super();
// 初始容量 < 0 抛出异常
if (initialCapacity < 0)
throw new IllegalArgumentException("Illegal Capacity: "+ initialCapacity);
// 初始化 elementData
this.elementData = new Object[initialCapacity];
// 初始化容量增量
this.capacityIncrement = capacityIncrement;
}
public Vector(Collection<? extends E> c) {
elementData = c.toArray();
elementCount = elementData.length;
// c.toArray might (incorrectly) not return Object[] (see 6260652)
if (elementData.getClass() != Object[].class)
elementData = Arrays.copyOf(elementData, elementCount, Object[].class);
}
Vector#size && Vector#capacity && Vector#isEmpty
size 方法是返回 Vector 中元素的数量。capacity 方法是返回 elementData 数组的数量,这个数量可能会比 elementCount 大。
protected int elementCount;
public synchronized int size() {
return elementCount;
}
public synchronized int capacity() {
return elementData.length;
}
public synchronized boolean isEmpty() {
return elementCount == 0;
}
Vector#add(E)
此方法在 JDK 1.2 后实现的,会将元素插入到 Vector 尾部。
public synchronized boolean add(E e) {
// modCount + 1
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;
// 如果 capacityIncrement > 0 则扩容后的长度是 oldCapacity + capacityIncrement,否则是 oldCapacity + oldCapacity
int newCapacity = oldCapacity + ((capacityIncrement > 0) ?
capacityIncrement : oldCapacity);
if (newCapacity - minCapacity < 0)
newCapacity = minCapacity;
if (newCapacity - MAX_ARRAY_SIZE > 0)
newCapacity = hugeCapacity(minCapacity);
// 使用 Arrays.copyOf 扩容 创建一个新数组并将原数组的数据复制到新数组
elementData = Arrays.copyOf(elementData, newCapacity);
}
private static int hugeCapacity(int minCapacity) {
if (minCapacity < 0) // overflow
throw new OutOfMemoryError();
return (minCapacity > MAX_ARRAY_SIZE) ?
Integer.MAX_VALUE :
MAX_ARRAY_SIZE;
}
Vector#add(int, E)
此方法在 JDK 1.2 后实现的,会将元素插入到 Vector 指定的 index,然后右移该 index 和右侧的所有元素。
public void add(int index, E element) {
insertElementAt(element, index);
}
public synchronized void insertElementAt(E obj, int index) {
// modCount + 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;
elementCount++;
}
Vector#get && Vector#set
get 和 set 方法都是通过数组下标方法指定的元素。
public synchronized E get(int index) {
if (index >= elementCount)
throw new ArrayIndexOutOfBoundsException(index);
return elementData(index);
}
public synchronized E set(int index, E element) {
if (index >= elementCount)
throw new ArrayIndexOutOfBoundsException(index);
E oldValue = elementData(index);
elementData[index] = element;
return oldValue;
}
E elementData(int index) {
return (E) elementData[index];
}
Vector#remove(java.lang.Object)
public boolean remove(Object o) {
return removeElement(o);
}
// 删除元素后会把被删除元素后面的所有元素向左移动一个位置
public synchronized boolean removeElement(Object obj) {
// modCount + 1
modCount++;
// 找到第一个出现此元素的下标
int i = indexOf(obj);
if (i >= 0) {
// 如果元素存在则删除
removeElementAt(i);
return true;
}
return false;
}
public int indexOf(Object o) {
// 从下标 0 开始找
return indexOf(o, 0);
}
// 返回下标
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;
}
public synchronized void removeElementAt(int index) {
// modCount + 1
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);
}
// 数量 - 1
elementCount--;
// 最后一个元素设置为 null
elementData[elementCount] = null; /* to let gc do its work */
}
Vector#remove(int)
public synchronized E remove(int index) {
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);
// 相当于 elementCount--;elementData[elementCount] = null;
elementData[--elementCount] = null; // Let gc do its work
return oldValue;
}
Vector#contains
public boolean contains(Object o) {
// 从索引 0 开始查找,时间复杂度是 O(n)
return indexOf(o, 0) >= 0;
}
Vector#lastIndexOf(java.lang.Object)
和 Vector#indexOf(java.lang.Object) 不同,此方法是逆序遍历数组。
public synchronized int lastIndexOf(Object o) {
return lastIndexOf(o, elementCount-1);
}
public synchronized int lastIndexOf(Object o, int index) {
if (index >= elementCount)
throw new IndexOutOfBoundsException(index + " >= "+ elementCount);
if (o == null) {
for (int i = index; i >= 0; i--)
if (elementData[i]==null)
return i;
} else {
for (int i = index; i >= 0; i--)
if (o.equals(elementData[i]))
return i;
}
return -1;
}
Vector#clear
public void clear() {
removeAllElements();
}
public synchronized void removeAllElements() {
modCount++;
// Let gc do its work
for (int i = 0; i < elementCount; i++)
elementData[i] = null;
elementCount = 0;
}
Vector#subList
返回的是经过 Collections.synchronizedList 包装的线程安全类,并不是 Vector 本身。
public synchronized List<E> subList(int fromIndex, int toIndex) {
return Collections.synchronizedList(super.subList(fromIndex, toIndex),
this);
}
迭代器
Vector 提供了 Itr 和 ListItr 两种迭代器。迭代器内部也是使用了 synchronized 关键字,并且锁的是 Vector 类对象。
final class ListItr extends Itr implements ListIterator<E> {
ListItr(int index) {
super();
cursor = index;
}
public boolean hasPrevious() {
return cursor != 0;
}
public int nextIndex() {
return cursor;
}
public int previousIndex() {
return cursor - 1;
}
public E previous() {
synchronized (Vector.this) {
checkForComodification();
int i = cursor - 1;
if (i < 0)
throw new NoSuchElementException();
cursor = i;
return elementData(lastRet = i);
}
}
public void set(E e) {
if (lastRet == -1)
throw new IllegalStateException();
synchronized (Vector.this) {
checkForComodification();
Vector.this.set(lastRet, e);
}
}
public void add(E e) {
int i = cursor;
synchronized (Vector.this) {
checkForComodification();
Vector.this.add(i, e);
expectedModCount = modCount;
}
cursor = i + 1;
lastRet = -1;
}
}
Vector#ensureCapacity
可以手动调用此方法进行一次扩容,避免添加数据过多频繁进行扩容操作,此操作也会增加 modCount。
public synchronized void ensureCapacity(int minCapacity) {
if (minCapacity > 0) {
modCount++;
ensureCapacityHelper(minCapacity);
}
}
Vector#setSize
此方法既可以支持扩容还可以修剪 Vector ,取决于你传入的 newSize 大小。
public synchronized void setSize(int newSize) {
modCount++;
// 如果新的长度大于当前元素数量则扩容
if (newSize > elementCount) {
ensureCapacityHelper(newSize);
} else {
// 如果新的长度小于当前元素数量则将 newSize 以后的元素都设为 null
for (int i = newSize ; i < elementCount ; i++) {
elementData[i] = null;
}
}
elementCount = newSize;
}
验证删除元素后 Vector 是否会自动缩容
通过结果可以得知删除了 Vector 里的元素,Vector 也并不会自动缩容,Vector 的内部数组仍然保持扩容后的大小,只不过数组里使用 null 进行填充 。
public static void test1(){
Vector<String> vector = new Vector<String>(2);
for (int i = 0; i < 10000; i++) {
vector.add(String.valueOf(i));
}
System.out.println(vector.size()); // 10000
System.out.println(vector.capacity()); // 16384
vector.remove(0);
System.out.println(vector.size()); // 9999
System.out.println(vector.capacity()); // 16384
ListIterator<String> iterator = vector.listIterator();
int i = 0;
while (iterator.hasNext()) {
iterator.next();
iterator.remove();
if (i++ == 9000) {
break;
}
}
System.out.println(vector.size()); // 998
System.out.println(vector.capacity()); // 16384
}
遍历方式
public static void testIterator() {
Vector<String> vector = new Vector<String>();
// 1.
for (String s : vector) {
}
// 2.
vector.forEach(new Consumer<String>() {
public void accept(String s) {
}
});
// 3. 效率最高
for (int i = 0; i < vector.size(); i++) {
}
// 4.
Iterator<String> iterator = vector.iterator();
while (iterator.hasNext()) {
iterator.next();
}
// 5. JDK 1.0 提供的 不建议使用
Enumeration<String> elements = vector.elements();
while (elements.hasMoreElements()) {
elements.nextElement();
}
}
Vector 与 ArrayList 的区别
- Vector 是线程安全的,是通过在方法使用 synchronized 达到线程安全效果,ArrayList 是线程非安全的。
- Vecto r可以指定增长因子,如果该增长因子指定了,那么扩容的时候会每次新的数组大小会在原数组的大小基础上加上增长因子;如果不指定增长因子,那么就给原数组大小 *2。