首先我们来看JDK源码中Java.util.Vector的代码,剔除所有的方法和静态变量,
Java.lang.Vector的核心代码如下:
- public class Vector<E>
- extends AbstractList<E>
- implements List<E>, RandomAccess, Cloneable, java.io.Serializable
- {
- protected Object[] elementData;
- /**
- * The number of valid components in this {@code Vector} object.
- * Components {@code elementData[0]} through
- * {@code elementData[elementCount-1]} are the actual items.
- *
- * @serial
- */
- protected int elementCount;
- protected int capacityIncrement; //可以设定固定增量
- }
通过上面的代码我们能看到他是数组存储。
它的构造方法有四个:
- /**
- * Constructs an empty vector with the specified initial capacity and
- * capacity increment.
- *
- * @param initialCapacity the initial capacity of the vector
- * @param capacityIncrement the amount by which the capacity is
- * increased when the vector overflows
- * @throws IllegalArgumentException if the specified initial capacity
- * is negative
- */
- public Vector(int initialCapacity, int capacityIncrement) { //设置初始容量,及固定增量
- super();
- if (initialCapacity < 0)
- throw new IllegalArgumentException("Illegal Capacity: "+
- initialCapacity);
- this.elementData = new Object[initialCapacity];
- this.capacityIncrement = capacityIncrement;
- }
- /**
- * Constructs an empty vector with the specified initial capacity and
- * with its capacity increment equal to zero.
- *
- * @param initialCapacity the initial capacity of the vector
- * @throws IllegalArgumentException if the specified initial capacity
- * is negative
- */
- public Vector(int initialCapacity) { //设置初始容量,不设定固定增量(如果不设定固定增量,则成倍增长)
- this(initialCapacity, 0);
- }
- /**
- * Constructs an empty vector so that its internal data array
- * has size {@code 10} and its standard capacity increment is
- * zero.
- */
- public Vector() { //不设置初始容量,初始容量为10,不设定固定增量(如果不设定固定增量,则成倍增长)
- this(10);
- }
- /**
- * Constructs a vector containing the elements of the specified
- * collection, in the order they are returned by the collection's
- * iterator.
- *
- * @param c the collection whose elements are to be placed into this
- * vector
- * @throws NullPointerException if the specified collection is null
- * @since 1.2
- */
- 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类的 add(E e)方法的源代码:
- /**
- * Appends the specified element to the end of this Vector.
- *
- * @param e element to be appended to this Vector
- * @return {@code true} (as specified by {@link Collection#add})
- * @since 1.2
- */
- public synchronized boolean add(E e) { //从这里的synchronized我们可以知道他是线程同步的
- modCount++;
- ensureCapacityHelper(elementCount + 1); //是否需要扩容
- elementData[elementCount++] = e; //后放的永远在最后一位
- return true; // 好像他永远返回true 哦
- }
- /**
- * This implements the unsynchronized semantics of ensureCapacity.
- * Synchronized methods in this class can internally call this
- * method for ensuring capacity without incurring the cost of an
- * extra synchronization.
- *
- * @see #ensureCapacity(int)
- */
- private void ensureCapacityHelper(int minCapacity) {
- int oldCapacity = elementData.length;
- if (minCapacity > oldCapacity) { //最新的值如果超出现有容量 扩容
- Object[] oldData = elementData;
- int newCapacity = (capacityIncrement > 0) ?
- (oldCapacity + capacityIncrement) : (oldCapacity * 2); //如果固定增量不为0则增长固定增量,否则成倍增长
- if (newCapacity < minCapacity) { //新容量还小,这个可能性应该不大
- newCapacity = minCapacity;
- }
- elementData = Arrays.copyOf(elementData, newCapacity); // 改变数组的大小,这个方法可以记一下
- }
- }
下面我们再看一下 Vector类的 remove(Object o)方法的源代码:
- /**
- * Removes the first occurrence of the specified element in this Vector
- * If the Vector does not contain the element, it is unchanged. More
- * formally, removes the element with the lowest index i such that
- * {@code (o==null ? get(i)==null : o.equals(get(i)))} (if such
- * an element exists).
- *
- * @param o element to be removed from this Vector, if present
- * @return true if the Vector contained the specified element
- * @since 1.2
- */
- public boolean remove(Object o) {
- return removeElement(o); //他是调用的removeElement
- }
- /**
- * Removes the first (lowest-indexed) occurrence of the argument
- * from this vector. If the object is found in this vector, each
- * component in the vector with an index greater or equal to the
- * object's index is shifted downward to have an index one smaller
- * than the value it had previously.
- *
- * <p>This method is identical in functionality to the
- * {@link #remove(Object)} method (which is part of the
- * {@link List} interface).
- *
- * @param obj the component to be removed
- * @return {@code true} if the argument was a component of this
- * vector; {@code false} otherwise.
- */
- public synchronized boolean removeElement(Object obj) {
- modCount++;
- int i = indexOf(obj); //查找obj第一次出现的位置,(Vector是允许重复值的)
- if (i >= 0) {
- removeElementAt(i);
- return true;
- }
- return false;
- }
- /**
- * Deletes the component at the specified index. Each component in
- * this vector with an index greater or equal to the specified
- * {@code index} is shifted downward to have an index one
- * smaller than the value it had previously. The size of this vector
- * is decreased by {@code 1}.
- *
- * <p>The index must be a value greater than or equal to {@code 0}
- * and less than the current size of the vector.
- *
- * <p>This method is identical in functionality to the {@link #remove(int)}
- * method (which is part of the {@link List} interface). Note that the
- * {@code remove} method returns the old value that was stored at the
- * specified position.
- *
- * @param index the index of the object to remove
- * @throws ArrayIndexOutOfBoundsException if the index is out of range
- * ({@code index < 0 || index >= size()})
- */
- 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; // 数组中index以后的元素个数
- if (j > 0) {
- System.arraycopy(elementData, index + 1, elementData, index, j); // 数组中index以后的元素,整体前移,(这个方法挺有用的!!)
- }
- elementCount--;
- elementData[elementCount] = null; /* to let gc do its work */
- }
从上面的代码我们可以看出 Vector每次只删除最靠前的那个相符的变量。
下面我们再看一下 Vector类的 equals(Object o)方法的源代码:
- /**
- * Compares the specified Object with this Vector for equality. Returns
- * true if and only if the specified Object is also a List, both Lists
- * have the same size, and all corresponding pairs of elements in the two
- * Lists are <em>equal</em>. (Two elements {@code e1} and
- * {@code e2} are <em>equal</em> if {@code (e1==null ? e2==null :
- * e1.equals(e2))}.) In other words, two Lists are defined to be
- * equal if they contain the same elements in the same order.
- *
- * @param o the Object to be compared for equality with this Vector
- * @return true if the specified Object is equal to this Vector
- */
- public synchronized boolean equals(Object o) {
- return super.equals(o); //他是调用的父类AbstractList的equals
- }
- /**
- * Compares the specified object with this list for equality. Returns
- * {@code true} if and only if the specified object is also a list, both
- * lists have the same size, and all corresponding pairs of elements in
- * the two lists are <i>equal</i>. (Two elements {@code e1} and
- * {@code e2} are <i>equal</i> if {@code (e1==null ? e2==null :
- * e1.equals(e2))}.) In other words, two lists are defined to be
- * equal if they contain the same elements in the same order.<p>
- *
- * This implementation first checks if the specified object is this
- * list. If so, it returns {@code true}; if not, it checks if the
- * specified object is a list. If not, it returns {@code false}; if so,
- * it iterates over both lists, comparing corresponding pairs of elements.
- * If any comparison returns {@code false}, this method returns
- * {@code false}. If either iterator runs out of elements before the
- * other it returns {@code false} (as the lists are of unequal length);
- * otherwise it returns {@code true} when the iterations complete.
- *
- * @param o the object to be compared for equality with this list
- * @return {@code true} if the specified object is equal to this list
- */
- public boolean equals(Object o) {
- if (o == this)
- return true;
- if (!(o instanceof List))
- return false;
- ListIterator<E> e1 = listIterator();
- ListIterator e2 = ((List) o).listIterator();
- while(e1.hasNext() && e2.hasNext()) {
- E o1 = e1.next();
- Object o2 = e2.next();
- if (!(o1==null ? o2==null : o1.equals(o2))) //判断了三次
- //这个方法挺不错的,应该比if(!(null==o1&&null==o2)||(null!=o1&&o1.equals(o2)))(大于等于三次判断)
- return false;
- }
- return !(e1.hasNext() || e2.hasNext()); //长度是否一致
- }
下面我们再看一下 Vector类的 hashCode()方法的源代码:
- /**
- * Returns the hash code value for this Vector.
- */
- public synchronized int hashCode() {
- return super.hashCode(); //他也是调用的父类AbstractList的hashCode
- }
- /**
- * Returns the hash code value for this list.
- *
- * <p>This implementation uses exactly the code that is used to define the
- * list hash function in the documentation for the {@link List#hashCode}
- * method.
- *
- * @return the hash code value for this list
- */
- public int hashCode() {
- int hashCode = 1;
- Iterator<E> i = iterator();
- while (i.hasNext()) {
- E obj = i.next();
- hashCode = 31*hashCode + (obj==null ? 0 : obj.hashCode());
- // 在这里用obj,比用i.next()效率高一倍(一直没明白他这为什么用31)
- }
- return hashCode;
- }
- 顶
- 0
- 踩
Vector的底层也是使用数组实现的,而且继承了AbstractList,实现了List接口,看着Vector和ArrayList很像:底层都是用数组实现,都继承了AbstractList,实现了List接口。所以搜了下看看Vector和ArrayList有什么区别。
在The Java Programming Language (Addison-Wesley, June 2000) 中Ken Arnold, James Gosling, 和 David Holmes 是这样描述Vector的,它是更ArrayList类似的一个东西,所以从API的观点来看,它们俩是很相似的。但是,它们之间还是有些微的差别的。
1. 同步性
Vectors是可同步化的,意思就是说,任何操作Vector的内容的方法都是线程安全的,相反的,另一方面,ArrayList是不可同步化的,所以也不是线程安全的。如果你知道了这些的话,你就会发现,Vector的同步会让它在性能发方面有一些小问题。所以,如果你不需要线程安全的话,那么就使用ArrayList吧。为什么要为没有必要的同步付出代价呢?
2. 数据增长
实际上,不管是ArrayList还是Vector,在它们内部都是使用一个数组来保存数据的。开发过程中,在使用它们任何一个的时候,你都需要记住这一点。你在往一个ArrayList或者Vector里插入一个元素的时候,如果内部数组空间不够了,ArrayList或者Vector就要扩展它的大小。Vector在默认情况下是增长一倍的大小,而ArrayList增加50%的大小。只要你合理的使用这些类,你就可以结束你在增加新的元素的时候所付出的性能代价。设置初始化容量为你编程过程中所能用到的最大的容量总是最好的办法。精确的指定容量,你可以避免以后改变内部Array容量,所要付出的代价。如果你并不知道到底有多少个数据,当是你知道数据的增长率,Vector确实有一点点优势,因为你可以指定增加值(作者说的方法应该是setSize(int newSize) Sets the size of this vector.)。
3. 查询,插入,删除对象的效率
ArrayList和Vector在从指定位置取得元素,从容器的末尾增加和删除元素都非常的有效,所有的这些操作都能在一个常数级的时间O(1)内完成。但是从一个其他的位置增加和删除一个元素就显得颇为费时,差不多需要的时间为O(n-i),这里的n代表元素个数,i代表要增加和删除的元素所在的位置。这些操作需花费更多的时间,因为你需要挨个移动i和更高位置的元素。那么,以上这些到底说明了什么呢?
这意味着,如果你取得一个元素,或者从数组末尾增加或删除一个元素的话,随便你使用Vector和ArrayList。如果你想要对数组内容做其他操作的话,那么就为自己好另一个容器吧。比如说,LinkedList可以在常数级时间(O1)内为任意一个位置的元素增加和删除。
(这个涉及到ArrayList和LinkedList的实现不同,一个是数组,一个是链表了,这个以后再写一篇博客说说。)
最后,Practical Java (Addison-Wesley, Feb. 2000) Peter Haggar 里的“实践41“建议你使用一个普通的原始的数组来代替Vector和ArrayListe,特别是对效率优先的代码来说。通过使用数组(array),你可以避免同步,额外的方法调用,非理想化的大小改变。你付出的只是额外的开发时间。
看来常用的还是ArrayList,只有在线程安全时才会去使用Vector