JDK源码分析——Java.util.Vector的浅析

首先我们来看JDK源码中Java.util.Vector的代码,剔除所有的方法和静态变量,

Java.lang.Vector的核心代码如下:

[java]  view plain copy
  1. public class Vector<E>  
  2.     extends AbstractList<E>  
  3.     implements List<E>, RandomAccess, Cloneable, java.io.Serializable  
  4. {  
  5.      
  6.     protected Object[] elementData;  
  7.   
  8.     /** 
  9.      * The number of valid components in this {@code Vector} object. 
  10.      * Components {@code elementData[0]} through 
  11.      * {@code elementData[elementCount-1]} are the actual items. 
  12.      * 
  13.      * @serial 
  14.      */  
  15.     protected int elementCount;  
  16.   
  17.     protected int capacityIncrement; //可以设定固定增量  
  18. }  

通过上面的代码我们能看到他是数组存储。

它的构造方法有四个:

[java]  view plain copy
  1. /** 
  2.     * Constructs an empty vector with the specified initial capacity and 
  3.     * capacity increment. 
  4.     * 
  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: "+  
  15.                                               initialCapacity);  
  16. this.elementData = new Object[initialCapacity];  
  17. this.capacityIncrement = capacityIncrement;  
  18.    }  
  19.   
  20.    /** 
  21.     * Constructs an empty vector with the specified initial capacity and 
  22.     * with its capacity increment equal to zero. 
  23.     * 
  24.     * @param   initialCapacity   the initial capacity of the vector 
  25.     * @throws IllegalArgumentException if the specified initial capacity 
  26.     *         is negative 
  27.     */  
  28.    public Vector(int initialCapacity) {    //设置初始容量,不设定固定增量(如果不设定固定增量,则成倍增长)  
  29. this(initialCapacity, 0);  
  30.    }  
  31.   
  32.    /** 
  33.     * Constructs an empty vector so that its internal data array 
  34.     * has size {@code 10} and its standard capacity increment is 
  35.     * zero. 
  36.     */  
  37.    public Vector() {    //不设置初始容量,初始容量为10,不设定固定增量(如果不设定固定增量,则成倍增长)  
  38. this(10);  
  39.    }  
  40.   
  41.    /** 
  42.     * Constructs a vector containing the elements of the specified 
  43.     * collection, in the order they are returned by the collection's 
  44.     * iterator. 
  45.     * 
  46.     * @param c the collection whose elements are to be placed into this 
  47.     *       vector 
  48.     * @throws NullPointerException if the specified collection is null 
  49.     * @since   1.2 
  50.     */  
  51.    public Vector(Collection<? extends E> c) {  //以集合初始化  
  52. elementData = c.toArray();  
  53. elementCount = elementData.length;  
  54. // c.toArray might (incorrectly) not return Object[] (see 6260652)  
  55. if (elementData.getClass() != Object[].class)  
  56.     elementData = Arrays.copyOf(elementData, elementCount, Object[].class);  
  57.    }  

下面我们再看一下  Vector类的 add(E e)方法的源代码:

[java]  view plain copy
  1.   /** 
  2.     * Appends the specified element to the end of this Vector. 
  3.     * 
  4.     * @param e element to be appended to this Vector 
  5.     * @return {@code true} (as specified by {@link Collection#add}) 
  6.     * @since 1.2 
  7.     */  
  8.    public synchronized boolean add(E e) {    //从这里的synchronized我们可以知道他是线程同步的  
  9. modCount++;  
  10. ensureCapacityHelper(elementCount + 1);   //是否需要扩容  
  11. elementData[elementCount++] = e;   //后放的永远在最后一位  
  12.        return true;   // 好像他永远返回true 哦  
  13.    }  

[java]  view plain copy
  1.    /** 
  2.     * This implements the unsynchronized semantics of ensureCapacity. 
  3.     * Synchronized methods in this class can internally call this 
  4.     * method for ensuring capacity without incurring the cost of an 
  5.     * extra synchronization. 
  6.     * 
  7.     * @see #ensureCapacity(int) 
  8.     */  
  9.    private void ensureCapacityHelper(int minCapacity) {  
  10. int oldCapacity = elementData.length;  
  11. if (minCapacity > oldCapacity) {    //最新的值如果超出现有容量 扩容  
  12.     Object[] oldData = elementData;  
  13.     int newCapacity = (capacityIncrement > 0) ?  
  14.     (oldCapacity + capacityIncrement) : (oldCapacity * 2);  //如果固定增量不为0则增长固定增量,否则成倍增长  
  15.   
  16.         if (newCapacity < minCapacity) {   //新容量还小,这个可能性应该不大  
  17.     newCapacity = minCapacity;  
  18.     }  
  19.            elementData = Arrays.copyOf(elementData, newCapacity);   // 改变数组的大小,这个方法可以记一下  
  20. }  
  21.    }  

下面我们再看一下  Vector类的 remove(Object o)方法的源代码:

[java]  view plain copy
  1. /** 
  2.     * Removes the first occurrence of the specified element in this Vector 
  3.     * If the Vector does not contain the element, it is unchanged.  More 
  4.     * formally, removes the element with the lowest index i such that 
  5.     * {@code (o==null ? get(i)==null : o.equals(get(i)))} (if such 
  6.     * an element exists). 
  7.     * 
  8.     * @param o element to be removed from this Vector, if present 
  9.     * @return true if the Vector contained the specified element 
  10.     * @since 1.2 
  11.     */  
  12.    public boolean remove(Object o) {  
  13.        return removeElement(o);      //他是调用的removeElement  
  14.    }  
  15.    
  16. /** 
  17.     * Removes the first (lowest-indexed) occurrence of the argument 
  18.     * from this vector. If the object is found in this vector, each 
  19.     * component in the vector with an index greater or equal to the 
  20.     * object's index is shifted downward to have an index one smaller 
  21.     * than the value it had previously. 
  22.     * 
  23.     * <p>This method is identical in functionality to the 
  24.     * {@link #remove(Object)} method (which is part of the 
  25.     * {@link List} interface). 
  26.     * 
  27.     * @param   obj   the component to be removed 
  28.     * @return  {@code true} if the argument was a component of this 
  29.     *          vector; {@code false} otherwise. 
  30.     */  
  31.    public synchronized boolean removeElement(Object obj) {  
  32. modCount++;  
  33. int i = indexOf(obj);         //查找obj第一次出现的位置,(Vector是允许重复值的)  
  34. if (i >= 0) {  
  35.     removeElementAt(i);          
  36.     return true;  
  37. }  
  38. return false;  
  39.    }  
  40.   
  41. /** 
  42.     * Deletes the component at the specified index. Each component in 
  43.     * this vector with an index greater or equal to the specified 
  44.     * {@code index} is shifted downward to have an index one 
  45.     * smaller than the value it had previously. The size of this vector 
  46.     * is decreased by {@code 1}. 
  47.     * 
  48.     * <p>The index must be a value greater than or equal to {@code 0} 
  49.     * and less than the current size of the vector.  
  50.     * 
  51.     * <p>This method is identical in functionality to the {@link #remove(int)} 
  52.     * method (which is part of the {@link List} interface).  Note that the 
  53.     * {@code remove} method returns the old value that was stored at the 
  54.     * specified position. 
  55.     * 
  56.     * @param      index   the index of the object to remove 
  57.     * @throws ArrayIndexOutOfBoundsException if the index is out of range 
  58.     *          ({@code index < 0 || index >= size()}) 
  59.     */  
  60.    public synchronized void removeElementAt(int index) {  
  61. modCount++;  
  62. if (index >= elementCount) {  
  63.     throw new ArrayIndexOutOfBoundsException(index + " >= " +  
  64.                          elementCount);  
  65. }  
  66. else if (index < 0) {  
  67.     throw new ArrayIndexOutOfBoundsException(index);  
  68. }  
  69. int j = elementCount - index - 1;  // 数组中index以后的元素个数  
  70. if (j > 0) {  
  71.     System.arraycopy(elementData, index + 1, elementData, index, j);   // 数组中index以后的元素,整体前移,(这个方法挺有用的!!)  
  72. }  
  73. elementCount--;  
  74. elementData[elementCount] = null/* to let gc do its work */  
  75.    }  

从上面的代码我们可以看出 Vector每次只删除最靠前的那个相符的变量。

下面我们再看一下 Vector类的 equals(Object o)方法的源代码:

[java]  view plain copy
  1.    /** 
  2.     * Compares the specified Object with this Vector for equality.  Returns 
  3.     * true if and only if the specified Object is also a List, both Lists 
  4.     * have the same size, and all corresponding pairs of elements in the two 
  5.     * Lists are <em>equal</em>.  (Two elements {@code e1} and 
  6.     * {@code e2} are <em>equal</em> if {@code (e1==null ? e2==null : 
  7.     * e1.equals(e2))}.)  In other words, two Lists are defined to be 
  8.     * equal if they contain the same elements in the same order. 
  9.     * 
  10.     * @param o the Object to be compared for equality with this Vector 
  11.     * @return true if the specified Object is equal to this Vector 
  12.     */  
  13.    public synchronized boolean equals(Object o) {  
  14.        return super.equals(o);   //他是调用的父类AbstractList的equals  
  15.    }  
  16.   
  17.   
  18.    /** 
  19.     * Compares the specified object with this list for equality.  Returns 
  20.     * {@code true} if and only if the specified object is also a list, both 
  21.     * lists have the same size, and all corresponding pairs of elements in 
  22.     * the two lists are <i>equal</i>.  (Two elements {@code e1} and 
  23.     * {@code e2} are <i>equal</i> if {@code (e1==null ? e2==null : 
  24.     * e1.equals(e2))}.)  In other words, two lists are defined to be 
  25.     * equal if they contain the same elements in the same order.<p> 
  26.     * 
  27.     * This implementation first checks if the specified object is this 
  28.     * list. If so, it returns {@code true}; if not, it checks if the 
  29.     * specified object is a list. If not, it returns {@code false}; if so, 
  30.     * it iterates over both lists, comparing corresponding pairs of elements. 
  31.     * If any comparison returns {@code false}, this method returns 
  32.     * {@code false}.  If either iterator runs out of elements before the 
  33.     * other it returns {@code false} (as the lists are of unequal length); 
  34.     * otherwise it returns {@code true} when the iterations complete. 
  35.     * 
  36.     * @param o the object to be compared for equality with this list 
  37.     * @return {@code true} if the specified object is equal to this list 
  38.     */  
  39.    public boolean equals(Object o) {  
  40. if (o == this)  
  41.     return true;  
  42. if (!(o instanceof List))  
  43.     return false;  
  44.   
  45. ListIterator<E> e1 = listIterator();  
  46. ListIterator e2 = ((List) o).listIterator();  
  47. while(e1.hasNext() && e2.hasNext()) {  
  48.     E o1 = e1.next();  
  49.     Object o2 = e2.next();  
  50.     if (!(o1==null ? o2==null : o1.equals(o2)))   //判断了三次  
  51.                  //这个方法挺不错的,应该比if(!(null==o1&&null==o2)||(null!=o1&&o1.equals(o2)))(大于等于三次判断)  
  52.     return false;  
  53. }  
  54. return !(e1.hasNext() || e2.hasNext());  //长度是否一致  
  55.    }  

下面我们再看一下  Vector类的 hashCode()方法的源代码:

[java]  view plain copy
  1.     /** 
  2.      * Returns the hash code value for this Vector. 
  3.      */  
  4.     public synchronized int hashCode() {  
  5.         return super.hashCode();   //他也是调用的父类AbstractList的hashCode  
  6.     }  
  7.   
  8. /** 
  9.      * Returns the hash code value for this list. 
  10.      * 
  11.      * <p>This implementation uses exactly the code that is used to define the 
  12.      * list hash function in the documentation for the {@link List#hashCode} 
  13.      * method. 
  14.      * 
  15.      * @return the hash code value for this list 
  16.      */  
  17.     public int hashCode() {  
  18.     int hashCode = 1;  
  19.     Iterator<E> i = iterator();  
  20.     while (i.hasNext()) {  
  21.         E obj = i.next();  
  22.         hashCode = 31*hashCode + (obj==null ? 0 : obj.hashCode());  
  23.            // 在这里用obj,比用i.next()效率高一倍(一直没明白他这为什么用31)  
  24.     }  
  25.     return hashCode;  
  26.     }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值