Java集合学习(二)Vector源码学习

Java集合学习(二)Vector源码学习

前言

侵删,学习记录笔记。

Vector

与ArrayList类似,都是动态数组,底层也都是使用数组实现。但与ArrayList最大的区别在于Vector是同步的,线程安全的,不过由于线程安全,所以效率低。

成员变量


	//底层数组,用于存放数据
    protected Object[] elementData;

 	//数组当前元素数量
    protected int elementCount;

  	//容量扩容数,用于数组扩容
    protected int capacityIncrement;

    private static final long serialVersionUID = -2767605614048989439L;

构造函数

  
//Vector(初始容量,容量扩容数)
// 初始容量必须大于0否则报异常
public Vector(int initialCapacity, int capacityIncrement) {
        super();
        if (initialCapacity < 0)
            throw new IllegalArgumentException("Illegal Capacity: "+
                                               initialCapacity);
        this.elementData = new Object[initialCapacity];
        this.capacityIncrement = capacityIncrement;
    }

	//Vector(初始容量)
	//根据初始容量初始化数组,并将扩容数设置为0
    public Vector(int initialCapacity) {
        this(initialCapacity, 0);
    }


	//Vector()
	//初始化一个容量为10长度的数组,并将扩容数设置为0
    public Vector() {
        this(10);
    }

 	//Vector(Collection<? extends E> c)
	//根据Collection来构造Vector
    public Vector(Collection<? extends E> c) {
        elementData = c.toArray();
        elementCount = elementData.length;
        if (elementData.getClass() != Object[].class)
            elementData = Arrays.copyOf(elementData, elementCount, Object[].class);
    }

扩容函数

 
//数组的最大允许容量
//由于某些虚拟机在数组中保留一些头字,尝试分配这个最大存储容量
//可能会导致数组容量大于虚拟机的limit,导致OutOfMemoryError。
 private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;

//扩容函数
private void grow(int minCapacity) {
        int oldCapacity = elementData.length;
    	//如果扩容数小于0则新容量为旧容量的两倍,否则为旧容量+扩容数
        int newCapacity = oldCapacity + ((capacityIncrement > 0) ?
                                         capacityIncrement : oldCapacity);
    //当新容量仍然小于最小期望容量,则将新容量再次赋值为最小期望容量minCapacity
        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;
    }

添加函数

   //添加函数
	//先增加modCount(修改次数)
	//判断当前数组容量是否能容纳新元素,不够则进行扩容
	//添加元素,返回true
public synchronized boolean add(E e) {
        modCount++;
        ensureCapacityHelper(elementCount + 1);
        elementData[elementCount++] = e;
        return true;
    }

    //比较最小期望容量与数组长度,长度不够则扩容
    private void ensureCapacityHelper(int minCapacity) {
        if (minCapacity - elementData.length > 0)
            grow(minCapacity);
    }

查找函数

 //根据元素查找相应下标

public int indexOf(Object o) {
        return indexOf(o, 0);
    }

	//查找对应元素下标
	//index 开始下标,从index下标所在位置开始向下查找
    public synchronized int indexOf(Object o, int index) {
        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;
        }
        //查找失败返回-1
        return -1;
    }

删除函数

//根据元素删除数组相应元素
//先获取元素对应下标,然后删除对应下标元素
public synchronized boolean removeElement(Object obj) {
        modCount++;
        int i = indexOf(obj);
        if (i >= 0) {
            removeElementAt(i);
            return true;
        }
        return false;
    }


 public synchronized void removeElementAt(int index) {
        modCount++;
     //对index进行检验,如果大于数组实际元素数量,报越界异常
        if (index >= elementCount) {
            throw new ArrayIndexOutOfBoundsException(index + " >= " +
                                                     elementCount);
        }
     //检验index是否小于0,小于0报越界异常
        else if (index < 0) {
            throw new ArrayIndexOutOfBoundsException(index);
        }
     //将index下标后面的元素集体向前移动1下标,并将最后一位元素(elementData[--elementCount])设null
        int j = elementCount - index - 1;
        if (j > 0) {
            System.arraycopy(elementData, index + 1, elementData, index, j);
        }
        elementCount--;
        elementData[elementCount] = null; /* to let gc do its work */
    }

小结

Vector底层是由数组实现,所以查找效率高,但增删效率低,会导致频繁的内存变动

很多方法都是synchronized的,所以Vector是线程安全,但会影响效率

Vector的扩容与ArrayList不同,每次扩容都会增加大于等于自身长度的容量,很容易导致数组容量过大 (可以使用trimToSize()函数将容量调整为当前Vector实际元素的个数来释放空间

参考资料

Vector源码

Java8源码-Vector

为什么java不推荐使用vector

arrayList和vector的区别,为什么废弃掉了vector?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值