Java容器源码(二)——Vector(基于JDK8)

(一)、Vector概述

  • Vecotr是基于数组实现的,是一个动态数组,数组容量可以自动增长
  • Vector和ArrayList是十分类似的,不过Vector在每一个方法中都加入了Synchronized进行修饰,保证线程的安全(其实有时候也是不安全的,需要手动保证线程安全)
  • 因为在ArrayList中较为详细的分析了源码,而Vector又几乎一样,所以还没看过ArrayList源码的朋友建议可以先看下这篇文章:Java容器源码(一)——ArrayList(基于JDK8)。读完这篇文章之后再回来看Vector的源码就会特别轻松。

(二)、Vector源码解析

package java.util;

import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.function.UnaryOperator;

/**
*   Vector同样也是继承了AbstractList
*   实现了List接口,只是起到标识作用,AbstractList中已经实现了List接口
*   RandomAccess接口标识支持随机访问
*   Cloneable接口标识可以进行克隆
*   Serializable代表可序列化
*/
public class Vector<E>
    extends AbstractList<E>
    implements List<E>, RandomAccess, Cloneable, java.io.Serializable
{
    /**
    *   Object类型的数组,用于存放数据
     */
    protected Object[] elementData;

   
     /*
     *  数组的实际长度
     */
    protected int elementCount;

    /**
     * 数组扩容的时候的增长率
     */
    protected int capacityIncrement;

    /** 序列化的版本号 */
    private static final long serialVersionUID = -2767605614048989439L;

    /**
     * 带有两个参数的构造方法Vector(int initialCapacity, int capacityIncrement)
     */
    public Vector(int initialCapacity, int capacityIncrement) {
        super();
        if (initialCapacity < 0)
            throw new IllegalArgumentException("Illegal Capacity: "+
                                               initialCapacity);
        //根据initialCapacity初始化数组
        this.elementData = new Object[initialCapacity];
        //设定增长率
        this.capacityIncrement = capacityIncrement;
    }

    /**
     * 带有一个参数的构造方法Vector(int initialCapacity)
     */
    public Vector(int initialCapacity) {
        //调用上面那个带有两个参数的构造方法,增长率设置为0
        this(initialCapacity, 0);
    }

    /**
     * 无参构造方法Vector()
     */
    public Vector() {
        //默认设置的初始容量为10,调用一个参数的构造方法
        this(10);
    }

    /**
     * 将容器转换为Vector的构造方法Vector(Collection<? extends E> c)
     */
    public Vector(Collection<? extends E> c) {
        //将容器转换为数组,并赋值给elementData
        elementData = c.toArray();
        //设置数组的实际长度
        elementCount = elementData.length;
        // 如果c不是存放Object类型的数据,就转换为Object类型
        if (elementData.getClass() != Object[].class)
            elementData = Arrays.copyOf(elementData, elementCount, Object[].class);
    }

    /**
     * 将Vector的数据都拷贝到传入的数组anArray中
     */
    public synchronized void copyInto(Object[] anArray) {
        System.arraycopy(elementData, 0, anArray, 0, elementCount);
    }

    /**
     * 将数组的容量缩容为实际大小
     */
    public synchronized void trimToSize() {
        //修改次数+1
        modCount++;
        int oldCapacity = elementData.length;
        //如果实际大小小于容量,就进行缩容
        if (elementCount < oldCapacity) {
            elementData = Arrays.copyOf(elementData, elementCount);
        }
    }

    /**
     * 用于确保数组的容量足够
     */
    public synchronized void ensureCapacity(int minCapacity) {
        //如果需要的容量大于0
        if (minCapacity > 0) {
            //修改次数+1
            modCount++;
            //尝试进行扩容
            ensureCapacityHelper(minCapacity);
        }
    }

    /**
     * 检查是否需要进行扩容
     */
    private void ensureCapacityHelper(int minCapacity) {
        // 如果需要的容量大于实际容量
        if (minCapacity - elementData.length > 0)
            //进行扩容
            grow(minCapacity);
    }

    /**
     * 进行扩容操作
     */
    private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;

    private void grow(int minCapacity) {
        // overflow-conscious code
        int oldCapacity = elementData.length;
        //将oldCapacity扩容为oldCapacity+capacityIncrement
        int newCapacity = oldCapacity + ((capacityIncrement > 0) ?
                                         capacityIncrement : oldCapacity);
        if (newCapacity - minCapacity < 0)
            newCapacity = minCapacity;
        //如果新的容量大于数组的最大容量,就尝试调用更大的容量
        if (newCapacity - MAX_ARRAY_SIZE > 0)
            newCapacity = hugeCapacity(minCapacity);
        //创建一个新的数组,并实现复制
        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的实际大小
     */
    public synchronized void setSize(int newSize) {
        //修改次数+1
        modCount++;
        //容量不够,就进行扩容
        if (newSize > elementCount) {
            ensureCapacityHelper(newSize);
        } else {
            //容量太大了,进行缩容,没有用的位置设置为null,利于垃圾收集
            for (int i = newSize ; i < elementCount ; i++) {
                elementData[i] = null;
            }
        }
        elementCount = newSize;
    }

    /**
     * 获得Vector的容量
     */
    public synchronized int capacity() {
        return elementData.length;
    }

    /**
     * 获得Vector的实际大小
     */
    public synchronized int size() {
        return elementCount;
    }

    /**
     * 是否为空
     */
    public synchronized boolean isEmpty() {
        return elementCount == 0;
    }

    /**
     * 返回Vector的枚举类
     */
    public Enumeration<E> elements() {
        //匿名类实现
        return new Enumeration<E>() {
            //初始化为0
            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");
            }
        };
    }

    /**
     * 查看是否含有o对象
     */
    public boolean contains(Object o) {
        return indexOf(o, 0) >= 0;
    }

    /**
     * 查找o对象的位置
     */
    public int indexOf(Object o) {
        return indexOf(o, 0);
    }

    /**
     * 从index开始到末尾,查找o对象的位置
     */
    public synchronized int indexOf(Object o, int index) {
        //o对象是否为null
        if (o == null) {
            //for循环查找
            for (int i = index ; i < elementCount ; i++)
                if (elementData[i]==null)
                    return i;
        } else {
            //for循环查找
            for (int i = index ; i < elementCount ; i++)
                if (o.equals(elementData[i]))
                    return i;
        }
        //查找失败
        return -1;
    }

    /**
     * 从末尾开始查找o对象
     */
    public synchronized int lastIndexOf(Object o) {
        return lastIndexOf(o, elementCount-1);
    }

    /**
     * 从index位置到0,开始查找o对象 
     */
    public synchronized int lastIndexOf(Object o, int index) {
        if (index >= elementCount)
            throw new IndexOutOfBoundsException(index + " >= "+ elementCount);
        //o对象是否为null
        if (o == null) {
            //for循环查找
            for (int i = index; i >= 0; i--)
                if (elementData[i]==null)
                    return i;
        } else {
            //for循环查找
            for (int i = index; i >= 0; i--)
                if (o.equals(elementData[i]))
                    return i;
        }
        //查找失败
        return -1;
    }

    /**
     * 查找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);
    }

    /**
     * 设置第index位置上的元素为obj
     */
    public synchronized void setElementAt(E obj, int index) {
        if (index >= elementCount) {
            throw new ArrayIndexOutOfBoundsException(index + " >= " +
                                                     elementCount);
        }
        elementData[index] = obj;
    }

    /**
     * 删除index位置上的元素
     */
    public synchronized void removeElementAt(int index) {
        //修改次数+1
        modCount++;
        //位置不合法
        if (index >= elementCount) {
            throw new ArrayIndexOutOfBoundsException(index + " >= " +
                                                     elementCount);
        }
        //位置不合法
        else if (index < 0) {
            throw new ArrayIndexOutOfBoundsException(index);
        }
        //j是需要移动元素的个数
        int j = elementCount - index - 1;
        if (j > 0) {
            //index+1开始到末尾,全部元素向前移动1位
            System.arraycopy(elementData, index + 1, elementData, index, j);
        }
        //数目减少一个
        elementCount--;
        elementData[elementCount] = null; /* to let gc do its work */
    }

    /**
     * 在index位置上插入对象obj
     */
    public synchronized void insertElementAt(E obj, int index) {
        //修改次数+1
        modCount++;
        if (index > elementCount) {
            throw new ArrayIndexOutOfBoundsException(index
                                                     + " > " + elementCount);
        }
        //查看是否需要扩容
        ensureCapacityHelper(elementCount + 1);
        //将index开始到末尾,全部数据往后移动1位
        System.arraycopy(elementData, index, elementData, index + 1, elementCount - index);
        //数目+1
        elementData[index] = obj;
        elementCount++;
    }

    /**
     * 在数组的末尾添加上元素
     */
    public synchronized void addElement(E obj) {
        modCount++;
        ensureCapacityHelper(elementCount + 1);
        elementData[elementCount++] = obj;
    }

    /**
     * 删除数组中的第一个obk对象
     */
    public synchronized boolean removeElement(Object obj) {
        //修改次数+1
        modCount++;
        //查找位置
        int i = indexOf(obj);
        if (i >= 0) {
            //删除元素
            removeElementAt(i);
            return true;
        }
        return false;
    }

    /**
     * 删除所有元素
     */
    public synchronized void removeAllElements() {
        modCount++;
        // Let gc do its work
        for (int i = 0; i < elementCount; i++)
            elementData[i] = null;

        elementCount = 0;
    }

    /**
     * 重写Object对象的克隆方法
     */
    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);
        }
    }

    /**
     * 将Collection对象转换为数组
     */
    public synchronized Object[] toArray() {
        return Arrays.copyOf(elementData, elementCount);
    }

    /**
     * 将Collection对象转换为指定类型的数组
     */
    @SuppressWarnings("unchecked")
    public synchronized <T> T[] toArray(T[] a) {
        //如果传入的数组小于Vector的实际大小
        if (a.length < elementCount)
            //创建一个新数组并返回
            return (T[]) Arrays.copyOf(elementData, elementCount, a.getClass());
        //直接将Vector中的元素复制给数组a
        System.arraycopy(elementData, 0, a, 0, elementCount);

        if (a.length > elementCount)
            a[elementCount] = null;

        return a;
    }

   
    //返回指定位置的元素
    @SuppressWarnings("unchecked")
    E elementData(int index) {
        return (E) elementData[index];
    }

    /**
     * 获得指定位置的元素
     */
    public synchronized E get(int index) {
        if (index >= elementCount)
            throw new ArrayIndexOutOfBoundsException(index);

        return elementData(index);
    }

    /**
     * 设置index位置的元素为element
     */
    public synchronized E set(int index, E element) {
        if (index >= elementCount)
            throw new ArrayIndexOutOfBoundsException(index);

        E oldValue = elementData(index);
        elementData[index] = element;
        return oldValue;
    }

    /**
     * 添加元素
     */
    public synchronized boolean add(E e) {
        //修改次数+1
        modCount++;
        //查看是否需要扩容
        ensureCapacityHelper(elementCount + 1);
        //插入元素
        elementData[elementCount++] = e;
        return true;
    }

    /**
     * 删除o对象
     */
    public boolean remove(Object o) {
        return removeElement(o);
    }

    /**
     * 在指定位置插入元素
     */
    public void add(int index, E element) {
        insertElementAt(element, index);
    }

    /**
     * 删除index位置上的元素
     */
    public synchronized E remove(int index) {
        //修改次数+1
        modCount++;
        if (index >= elementCount)
            throw new ArrayIndexOutOfBoundsException(index);
        //获得删除之前的旧值
        E oldValue = elementData(index);

        //计算需要移动元素的个数
        int numMoved = elementCount - index - 1;
        if (numMoved > 0)
            //从index+1到末尾,全部元素向前移动1位
            System.arraycopy(elementData, index+1, elementData, index,
                             numMoved);
        //利于垃圾收集
        elementData[--elementCount] = null; // Let gc do its work

        return oldValue;
    }

    /**
     * 清空数组
     */
    public void clear() {
        removeAllElements();
    }


    /**
     * 查看是否数组是否包含c中的所有元素
     */
    public synchronized boolean containsAll(Collection<?> c) {
        return super.containsAll(c);
    }

    /**
     * 添加容器c中的所有元素
     */
    public synchronized boolean addAll(Collection<? extends E> c) {
        //修改次数+1
        modCount++;
        //将c转换为数组
        Object[] a = c.toArray();
        //长度
        int numNew = a.length;
        //查看是否需要扩容
        ensureCapacityHelper(elementCount + numNew);
        //进行复制
        System.arraycopy(a, 0, elementData, elementCount, numNew);
        elementCount += numNew;
        return numNew != 0;
    }

    /**
     * 删除所有集合c中的元素
     */
    public synchronized boolean removeAll(Collection<?> c) {
        return super.removeAll(c);
    }

    /**
     * 保留c中的元素
     */
    public synchronized boolean retainAll(Collection<?> c) {
        return super.retainAll(c);
    }

    /**
     * 从index位置到末尾,添加c中的所有元素
     */
    public synchronized boolean addAll(int index, Collection<? extends E> c) {
        //修改次数+1
        modCount++;
        if (index < 0 || index > elementCount)
            throw new ArrayIndexOutOfBoundsException(index);
        //将c转换为数组
        Object[] a = c.toArray();
        //长度
        int numNew = a.length;
        //查看是否需要扩容
        ensureCapacityHelper(elementCount + numNew);
        //计算需要移动元素的个数
        int numMoved = elementCount - index;
        if (numMoved > 0)
            //进行移动
            System.arraycopy(elementData, index, elementData, index + numNew,
                             numMoved);
        //将c拷贝进数组中
        System.arraycopy(a, 0, elementData, index, numNew);
        elementCount += numNew;
        return numNew != 0;
    }

    /**
     * 是否相等
     */
    public synchronized boolean equals(Object o) {
        return super.equals(o);
    }

    /**
     * 返回该对象的哈希值
     */
    public synchronized int hashCode() {
        return super.hashCode();
    }

    /**
     * 转为字符串
     */
    public synchronized String toString() {
        return super.toString();
    }

    /**
     * 从fromIndex到toIndex截断元素
     */
    public synchronized List<E> subList(int fromIndex, int toIndex) {
        return Collections.synchronizedList(super.subList(fromIndex, toIndex),
                                            this);
    }

    /**
     * 删除指定范围中的元素
     */
    protected synchronized void removeRange(int fromIndex, int toIndex) {
        //修改次数+1
        modCount++;
        //计算需要移动的元素个数
        int numMoved = elementCount - toIndex;
        //移动元素
        System.arraycopy(elementData, toIndex, elementData, fromIndex,
                         numMoved);

        //方便垃圾回收
        int newElementCount = elementCount - (toIndex-fromIndex);
        while (elementCount != newElementCount)
            elementData[--elementCount] = null;
    }

    /**
     * 序列化操作
     */
    private void writeObject(java.io.ObjectOutputStream s)
            throws java.io.IOException {
        final java.io.ObjectOutputStream.PutField fields = s.putFields();
        final Object[] data;
        synchronized (this) {
            fields.put("capacityIncrement", capacityIncrement);
            fields.put("elementCount", elementCount);
            data = elementData.clone();
        }
        fields.put("elementData", data);
        s.writeFields();
    }

}

(三)、ArrayList与Vector

  1. Vector其实是遗留类,因为方法都加上了synchroized语句,在多线程环境下效率不高,现在大多不再使用了。
  2. ArrayList与Vector拥有相同的扩容机制
  3. 不同点在于ArrayList没有设置增长率,默认扩容为1.5倍。Vector设置了增长率
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,560评论 4 361
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,104评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,297评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,869评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,275评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,563评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,833评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,543评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,245评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,512评论 2 244
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,011评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,359评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,006评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,062评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,825评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,590评论 2 273
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,501评论 2 268

推荐阅读更多精彩内容