其他分享
首页 > 其他分享> > JDK14中Vector类的动态扩容简单介绍

JDK14中Vector类的动态扩容简单介绍

作者:互联网

JDK14中Vector类的动态扩容

Vector类的动态扩容

源码分析

无参构造方法

/**
 * Constructs an empty vector so that its internal data array
 * has size {@code 10} and its standard capacity increment is
 * zero.
 */
public Vector() {
    this(10);
}

扩容

/**
 * 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) {
    modCount++;
    add(e, elementData, elementCount);
    return true;
}

/**
 * This helper method split out from add(E) to keep method
 * bytecode size under 35 (the -XX:MaxInlineSize default value),
 * which helps when add(E) is called in a C1-compiled loop.
 */
private void add(E e, Object[] elementData, int s) {
    if (s == elementData.length)
        elementData = grow();
    elementData[s] = e;
    elementCount = s + 1;
}

private Object[] grow() {
    return grow(elementCount + 1);
}
/**
 * Increases the capacity to ensure that it can hold at least the
 * number of elements specified by the minimum capacity argument.
 *
 * @param minCapacity the desired minimum capacity
 * @throws OutOfMemoryError if minCapacity is less than zero
 */
private Object[] grow(int minCapacity) {
    int oldCapacity = elementData.length;
    int newCapacity = ArraysSupport.newLength(oldCapacity,
            minCapacity - oldCapacity, /* minimum growth */
            capacityIncrement > 0 ? capacityIncrement : oldCapacity
                                       /* preferred growth */);
    return elementData = Arrays.copyOf(elementData, newCapacity);
}
/**
 * Calculates a new array length given an array's current length, a preferred
 * growth value, and a minimum growth value.  If the preferred growth value
 * is less than the minimum growth value, the minimum growth value is used in
 * its place.  If the sum of the current length and the preferred growth
 * value does not exceed {@link #MAX_ARRAY_LENGTH}, that sum is returned.
 * If the sum of the current length and the minimum growth value does not
 * exceed {@code MAX_ARRAY_LENGTH}, then {@code MAX_ARRAY_LENGTH} is returned.
 * If the sum does not overflow an int, then {@code Integer.MAX_VALUE} is
 * returned.  Otherwise, {@code OutOfMemoryError} is thrown.
 *
 * @param oldLength   current length of the array (must be non negative)
 * @param minGrowth   minimum required growth of the array length (must be
 *                    positive)
 * @param prefGrowth  preferred growth of the array length (ignored, if less
 *                    then {@code minGrowth})
 * @return the new length of the array
 * @throws OutOfMemoryError if increasing {@code oldLength} by
 *                    {@code minGrowth} overflows.
 */
public static int newLength(int oldLength, int minGrowth, int prefGrowth) {
    // assert oldLength >= 0
    // assert minGrowth > 0

    int newLength = Math.max(minGrowth, prefGrowth) + oldLength;
    if (newLength - MAX_ARRAY_LENGTH <= 0) {
        return newLength;
    }
    return hugeLength(oldLength, minGrowth);
}

Vector类和ArrayList类的区别

  1. vector是线程(Thread)同步(Synchronized)的,所以它也是线程安全的;而Arraylist是线程异步(ASynchronized)的,是不安全的。如果不考虑到线程的安全因素,一般用Arraylist效率比较高。
  2. 当集合中的元素数量大于当前集合的长度时,Vector会扩容为原来的2倍,而ArrayList只会扩容为原来的1.5倍。如果在集合中使用数据量比较大的数据,用vector有一定的优势。

在这里插入图片描述

标签:扩容,code,Vector,int,JDK14,elementData,length,growth
来源: https://blog.csdn.net/Caroline_13/article/details/119250571