集合番@Vector一文通(1.7版)

487 阅读3分钟
原文链接: www.zybuluo.com

1.什么是Vector

  • 矢量队列,作用等效于ArrayList,线程安全
  • 官方不推荐使用该类,非线程安全推荐 ArrayList,线程安全推荐 CopyOnWriteList
  • 区别于ArrayList,几乎所有public方法全部由 syncronized修饰,因此线程安全
  • 由于该类跟ArrayList代码基本一致(后者是前者的非同步代替类),因此仅仅只解析部分有差异的方法
  • 队列详情解析请参见笔者的 ArrayList一文通

2.Vector的数据结构

  • 类定义
  1. public class Vector<E>
  2. extends AbstractList<E>
  3. implements List<E>, RandomAccess, Cloneable, java.io.Serializable
  • 继承 AbstractList,实现了 List,它是一个数组队列,提供了相关的添加、删除、修改、遍历等功能
  • 实现 RandmoAccess 接口,实现快速随机访问:通过元素的序号快速获取元素对象
  • 实现 Cloneable 接口,重写 clone(),能被克隆(浅拷贝)
  • 实现 java.io.Serializable 接口,支持序列化
  • 重要全局变量
  1. /**
  2. * The array buffer into which the components of the vector are stored.
  3. * The capacity of the vector is the length of this array buffer,
  4. * and is at least large enough to contain all the vector's elements.
  5. * 底层数据结构为动态数组
  6. * <p>Any array elements following the last element in the Vector are null.
  7. * @serial
  8. */
  9. protected Object[] elementData;
  10. /**
  11. * The number of valid components in this {@code Vector} object.
  12. * Components {@code elementData[0]} through
  13. * {@code elementData[elementCount-1]} are the actual items.
  14. * 数组长度 == 数组的length == ArrayList的size()
  15. * @serial
  16. */
  17. protected int elementCount;
  18. /**
  19. * The amount by which the capacity of the vector is automatically
  20. * incremented when its size becomes greater than its capacity. If
  21. * the capacity increment is less than or equal to zero, the capacity
  22. * of the vector is doubled each time it needs to grow.
  23. * 当容量不够时,每次Vector容量增加时的增量值;若该值<=0,则默认自动扩容两倍
  24. * @serial
  25. */
  26. protected int capacityIncrement;
  • 构造器
  1. /**
  2. * Constructs an empty vector with the specified initial capacity and
  3. * capacity increment.
  4. * 创建一个指定容量和容量增量值的Vector
  5. * @param initialCapacity the initial capacity of the vector
  6. * @param capacityIncrement the amount by which the capacity is
  7. * increased when the vector overflows
  8. * @throws IllegalArgumentException if the specified initial capacity
  9. * is negative
  10. */
  11. public Vector(int initialCapacity, int capacityIncrement) {
  12. super();
  13. if (initialCapacity < 0)
  14. throw new IllegalArgumentException("Illegal Capacity: " + initialCapacity);
  15. this.elementData = new Object[initialCapacity];
  16. this.capacityIncrement = capacityIncrement;
  17. }
  18. /**
  19. * Constructs an empty vector with the specified initial capacity and
  20. * with its capacity increment equal to zero.
  21. * 创建一个指定容量的Vector,容量不够时自动扩容两倍
  22. * @param initialCapacity the initial capacity of the vector
  23. * @throws IllegalArgumentException if the specified initial capacity
  24. * is negative
  25. */
  26. public Vector(int initialCapacity) {
  27. this(initialCapacity, 0);
  28. }
  29. /**
  30. * Constructs an empty vector so that its internal data array has size {@code 10}
  31. * and its standard capacity increment is zero.
  32. * 默认构造器,初始容量为10(跟ArrayList一样),容量不够时自动扩容两倍
  33. */
  34. public Vector() {
  35. this(10);
  36. }
  37. /**
  38. * Constructs a vector containing the elements of the specified collection,
  39. * in the order they are returned by the collection's iterator.
  40. * 创建一个包含collection的Vector
  41. * @param c the collection whose elements are to be placed into this vector
  42. * @throws NullPointerException if the specified collection is null
  43. * @since 1.2
  44. */
  45. public Vector(Collection<? extends E> c) {
  46. elementData = c.toArray();
  47. elementCount = elementData.length;
  48. // c.toArray might (incorrectly) not return Object[] (see 6260652)
  49. if (elementData.getClass() != Object[].class)
  50. elementData = Arrays.copyOf(elementData, elementCount, Object[].class);
  51. }

3.Vector的方法解析

  • grow方法解析
  1. /**
  2. * 数组自动扩容
  3. * 区别于ArrayList 的 (int)Math.floor(oldCapacity*1.5)
  4. * Vector的扩容方案为:
  5. * 当增值量 > 0 时,新容量=原容量+增值量
  6. * 当增值量 <=0 时,新容量=原容量*2(但采用的是加法而非更高效的位运算)
  7. */
  8. private void grow(int minCapacity) {
  9. // overflow-conscious code
  10. int oldCapacity = elementData.length;
  11. //重点差异
  12. int newCapacity = oldCapacity + ((capacityIncrement > 0) ? capacityIncrement : oldCapacity);
  13. if (newCapacity - minCapacity < 0)
  14. newCapacity = minCapacity;
  15. if (newCapacity - MAX_ARRAY_SIZE > 0)
  16. newCapacity = hugeCapacity(minCapacity);
  17. elementData = Arrays.copyOf(elementData, newCapacity);
  18. }
  • writeObject方法解析
  1. /**
  2. * Save the state of the {@code Vector} instance to a stream (that is, serialize it).
  3. * This method performs synchronization to ensure the consistency of the serialized data.
  4. * 比较有意思的是虽然Vector支持序列化,但并没有像ArrayList支持提供 writeObject() 和 readObject()
  5. * 来分别支持 序列化和反序列化 的方法级别的直接支持, 唯一的 序列化方法竟然只是private级别。
  6. * 因此想实现Vector的序列化和反序列化功能必须通过手动通过 ObjectOutputStream实现
  7. */
  8. private void writeObject(java.io.ObjectOutputStream s)
  9. throws java.io.IOException {
  10. final java.io.ObjectOutputStream.PutField fields = s.putFields();
  11. final Object[] data;
  12. //该方法使用同步代码块(synchronized的推荐使用方法) -- 具体请期待笔者的`并发包系列`
  13. synchronized (this) {
  14. fields.put("capacityIncrement", capacityIncrement);
  15. fields.put("elementCount", elementCount);
  16. data = elementData.clone();
  17. }
  18. fields.put("elementData", data);
  19. s.writeFields();
  20. }