java.util.Vector
- 介绍
- 类图
- 类
- 属性
- 构造函数
- 方法
- 容量相关方法
- 列表大小相关方法
- 清空列表
- 包含元素相关方法
- 克隆
- 转换成数组
- 添加、插入元素
- 查找元素
- 获取元素
- 设置、替换元素
- 移除元素
- 比较
- 迭代器
- 子列表(待完善)
- 增强循环
- 排序
- 总结
- Vector和ArrayList的区别(待完善)
介绍
- Vector-矢量
- 继承了 抽象列表类AbstractList
- 实现了 列表接口List
- 另外还实现了RandomAccess、Cloneable、Serializable
- Vector 实现了 一个动态数组。和 ArrayList 很相似,但是两者是不同的是,Vector是线程安全的,会在可能出现线程安全的方法前面加上synchronized关键字。
类图
类
Vector有私有内部类Itr、常量内部类ListItr 和 VectorSpliterator。
属性
// 存储 向量分量 的 数组缓冲区。
// 向量的容量 是 这个数组缓冲区 的 长度,并且 至少 足够大 以 包含 向量的所有元素。
// 向量中 最后一个元素 后面 的 任何元素 都是 空(null)。
protected Object[] elementData;
// 当前Vector对象中 的 元素有效个数。组件elementData[0]到elementData[elementCount-1] 是 实际的项。
protected int elementCount;
// 当 矢量的大小 大于 其容量时,其容量 自动递增的量。
// 如果 容量增量 小于或等于零,则 向量的容量 将在每次需要增长时 加倍。
protected int capacityIncrement;
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
构造函数
public Vector() {
// 走了下面这个构造方法,10 是 initialCapacity的 参数值
this(10);
}
public Vector(int initialCapacity) {
this(initialCapacity, 0);
}
// initialCapacity-初始容量
// capacityIncrement-增长容量
public Vector(int initialCapacity, int capacityIncrement) {
// TODO 先调用了 父类AbstractList 的 构造方法,为什么?
super();
if (initialCapacity < 0)
throw new IllegalArgumentException("Illegal Capacity: "+
initialCapacity);
this.elementData = new Object[initialCapacity];
this.capacityIncrement = capacityIncrement;
}
public Vector(Collection<? extends E> c) {
// 先把指定集合c转成数组并赋值给elementData
elementData = c.toArray();
// 得到指定集合c的长度
elementCount = elementData.length;
// c.toArray might (incorrectly) not return Object[] (see 6260652)
if (elementData.getClass() != Object[].class)
// 如果指定集合c的类型不是Object,就转成Object类型数组,并重新赋值给elementData
elementData = Arrays.copyOf(elementData, elementCount, Object[].class);
}
方法
容量相关方法
// 同步方法,返回Vector的元素数组的长度
public synchronized int capacity() {
return elementData.length;
}
// 同步方法
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)
// 如果 最小容量 比 元素长度 大,使用minCapacity,调用grow方法,对数组进行扩容
grow(minCapacity);
}
// 增加容量
private void grow(int minCapacity) {
// oldCapacity-现有元素长度
// overflow-conscious code
int oldCapacity = elementData.length;
// 如果 容量增长量 > 0,则使用容量增长量,否则使用现有容量
// newCapacity(新容量) = 现有容量 + 上面的计算结果
int newCapacity = oldCapacity + ((capacityIncrement > 0) ?
capacityIncrement : oldCapacity);
// 如果计算出来的新容量 比 参数最小容量 还小,用参数最小容量 作为 新容量
if (newCapacity - minCapacity < 0)
newCapacity = minCapacity;
// 如果计算出来的新容量 比 MAX_ARRAY_SIZE 还大,通过hugeCapacity计算新容量
if (newCapacity - MAX_ARRAY_SIZE > 0)
newCapacity = hugeCapacity(minCapacity);
// 把elementData 复制到 长度为 newCapacity 的新数组里
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;
}
列表大小相关方法
// 同步方法,设置列表指定大小
public synchronized void setSize(int newSize) {
modCount++;
if (newSize > elementCount) {
// 如果 新容量 比 元素个数 大,调用ensureCapacityHelper方法
ensureCapacityHelper(newSize);
} else {
// 如果 新容量 比 元素个数 小
// 循环元素数组,把newSize后面的元素都置为null
for (int i = newSize ; i < elementCount ; i++) {
elementData[i] = null;
}
}
// 更新元素个数
elementCount = newSize;
}
// 同步方法,返回元素个数
public synchronized int size() {
return elementCount;
}
// 同步方法,根据元素个数判断列表是否为空
public synchronized boolean isEmpty() {
return elementCount == 0;
}
// 同步方法
public synchronized void trimToSize() {
modCount++;
// 元素数组长度
int oldCapacity = elementData.length;
// 如果 元素个数 < 元素数组长度,注意,是 数组的长度
if (elementCount < oldCapacity) {
// 把elementData复制到一个新数组里,新数组的长度就是元素个数的多少
elementData = Arrays.copyOf(elementData, elementCount);
}
}
清空列表
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;
}
包含元素相关方法
public boolean contains(Object o) {
return indexOf(o, 0) >= 0;
}
public synchronized boolean containsAll(Collection<?> c) {
// 调用AbstractList的containsAll方法
return super.containsAll(c);
}
public int indexOf(Object o) {
return indexOf(o, 0);
}
// 同步方法
public synchronized int indexOf(Object o, int index) {
// 从Vector列表头部开始循环
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 int lastIndexOf(Object o) {
// 从Vector列表尾部(elementCount-1)开始循环
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;
}
克隆
// 同步方法
public synchronized Object clone() {
try {
@SuppressWarnings("unchecked")
Vector<E> v = (Vector<E>) super.clone();
v.elementData = Arrays.copyOf(elementData, elementCount);
v.modCount = 0;
return v;
} catch (CloneNotSupportedException e) {
// this shouldn't happen, since we are Cloneable
throw new InternalError(e);
}
}
转换成数组
public synchronized Object[] toArray() {
return Arrays.copyOf(elementData, elementCount);
}
// 同步方法
public synchronized <T> T[] toArray(T[] a) {
if (a.length < elementCount)
// 如果 参数数组a的长度 < 元素个数
// 复制 元素数组elementData 到一个 新数组(长度为elementCount) 并返回
return (T[]) Arrays.copyOf(elementData, elementCount, a.getClass());
// 如果 参数数组a的长度 >= 元素个数
// 复制elementData到参数数组a
System.arraycopy(elementData, 0, a, 0, elementCount);
// 如果 参数数组a的长度 > 元素个数,把a的最后一个元素设置为null
if (a.length > elementCount)
a[elementCount] = null;
return a;
}
添加、插入元素
// 同步方法,实现的是List中的方法
public synchronized boolean add(E e) {
modCount++;
ensureCapacityHelper(elementCount + 1);
// 把元素添加到元素数组尾部
elementData[elementCount++] = e;
return true;
}
// 同步方法,Vector自己的方法
public synchronized void addElement(E obj) {
modCount++;
ensureCapacityHelper(elementCount + 1);
elementData[elementCount++] = obj;
}
// 添加指定元素到指定位置
public void add(int index, E element) {
insertElementAt(element, index);
}
// 同步方法,在指定位置插入元素
public synchronized void insertElementAt(E obj, int index) {
modCount++;
if (index > elementCount) {
throw new ArrayIndexOutOfBoundsException(index
+ " > " + elementCount);
}
ensureCapacityHelper(elementCount + 1);
// 把数组从index开始向后的元素都挪动一个位置
System.arraycopy(elementData, index, elementData, index + 1, elementCount - index);
elementData[index] = obj;
// 元素个数+1
elementCount++;
}
// 同步方法
public synchronized boolean addAll(Collection<? extends E> c) {
modCount++;
Object[] a = c.toArray();
int numNew = a.length;
ensureCapacityHelper(elementCount + numNew);
System.arraycopy(a, 0, elementData, elementCount, numNew);
elementCount += numNew;
return numNew != 0;
}
// 同步方法
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)
// 把数组elementData从index开始的numMoved个元素 复制到elementData中,起始位置是指定位置(index + numNew)
System.arraycopy(elementData, index, elementData, index + numNew,
numMoved);
// 把数组a从0开始的numNew个元素 复制到elementData中,起始位置是指定位置index
System.arraycopy(a, 0, elementData, index, numNew);
elementCount += numNew;
return numNew != 0;
}
查找元素
E elementData(int index) {
return (E) elementData[index];
}
// 同步方法,获取指定位置的元素
public synchronized E elementAt(int index) {
if (index >= elementCount) {
throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount);
}
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);
}
// 获取所有的元素,实现了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");
}
};
}
获取元素
// 同步方法,获取指定索引位置的元素
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;
}
// 同步方法,设置 指定位置index上的元素为指定元素obj
public synchronized void setElementAt(E obj, int index) {
if (index >= elementCount) {
throw new ArrayIndexOutOfBoundsException(index + " >= " +
elementCount);
}
elementData[index] = obj;
}
public synchronized void replaceAll(UnaryOperator<E> operator) {
Objects.requireNonNull(operator);
final int expectedModCount = modCount;
final int size = elementCount;
for (int i=0; modCount == expectedModCount && i < size; i++) {
// 循环列表 针对每个元素 执行operator操作
elementData[i] = operator.apply((E) elementData[i]);
}
if (modCount != expectedModCount) {
throw new ConcurrentModificationException();
}
modCount++;
}
移除元素
// 同步方法,移除指定位置的元素
public synchronized E remove(int index) {
modCount++;
if (index >= elementCount)
throw new ArrayIndexOutOfBoundsException(index);
// oldValue-当前元素
E oldValue = elementData(index);
// 计算需要移动的元素个数
int numMoved = elementCount - index - 1;
if (numMoved > 0)
System.arraycopy(elementData, index+1, elementData, index,
numMoved);
elementData[--elementCount] = null; // Let gc do its work
return oldValue;
}
public boolean remove(Object o) {
return removeElement(o);
}
// 同步方法,移除指定元素
public synchronized boolean removeElement(Object obj) {
modCount++;
// 先获取指定元素所在的位置i
int i = indexOf(obj);
if (i >= 0) {
// 移除i位置上的元素,也就是移除obj
removeElementAt(i);
return true;
}
return false;
}
// 同步方法,移除指定位置的元素
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);
}
// 元素个数自减1
elementCount--;
// 把最后一个位置 置为空元素
elementData[elementCount] = null; /* to let gc do its work */
}
public synchronized boolean removeAll(Collection<?> c) {
return super.removeAll(c);
}
// 同步方法,移除列表所有元素
public synchronized void removeAllElements() {
modCount++;
// Let gc do its work
for (int i = 0; i < elementCount; i++)
elementData[i] = null;
elementCount = 0;
}
protected synchronized void removeRange(int fromIndex, int toIndex) {
modCount++;
// 计算需要移动的元素个数
int numMoved = elementCount - toIndex;
// 把从toIndex开始的numMoved个元素往前移动
System.arraycopy(elementData, toIndex, elementData, fromIndex,
numMoved);
// Let gc do its work
int newElementCount = elementCount - (toIndex-fromIndex);
while (elementCount != newElementCount)
elementData[--elementCount] = null;
}
比较
// 同步方法
public synchronized boolean equals(Object o) {
return super.equals(o);
}
// 同步方法
public synchronized int hashCode() {
return super.hashCode();
}
public synchronized String toString() {
return super.toString();
}
迭代器
public synchronized Iterator<E> iterator() {
return new Itr();
}
public synchronized ListIterator<E> listIterator() {
return new ListItr(0);
}
public synchronized ListIterator<E> listIterator(int index) {
if (index < 0 || index > elementCount)
throw new IndexOutOfBoundsException("Index: "+index);
return new ListItr(index);
}
子列表(待完善)
// 同步方法
public synchronized List<E> subList(int fromIndex, int toIndex) {
// 使用父类AbstractList中的subList方法
return Collections.synchronizedList(super.subList(fromIndex, toIndex),
this);
}
增强循环
public synchronized void forEach(Consumer<? super E> action) {
Objects.requireNonNull(action);
final int expectedModCount = modCount;
@SuppressWarnings("unchecked")
final E[] elementData = (E[]) this.elementData;
final int elementCount = this.elementCount;
for (int i=0; modCount == expectedModCount && i < elementCount; i++) {
action.accept(elementData[i]);
}
if (modCount != expectedModCount) {
throw new ConcurrentModificationException();
}
}
排序
// 同步方法
public synchronized void sort(Comparator<? super E> c) {
final int expectedModCount = modCount;
// 使用数组的排序方法 + 比较器c 针对 从0-elementCount之间的元素进行处理
Arrays.sort((E[]) elementData, 0, elementCount, c);
if (modCount != expectedModCount) {
throw new ConcurrentModificationException();
}
modCount++;
}
总结
继承Vector的有:
- 栈-Stack
Vector和ArrayList的区别(待完善)