List集合之Vector分析

172 阅读1分钟

List集合之Vector分析

Vector的底层实现以及结构与ArrayList完全相同,只是在某一些细节上会有所不同。这些细节主要有:

  • 线程安全
  • 扩容大小

线程安全

我们知道ArrayList是线程不安全的,只能在单线程环境下使用。而Vector则是线程安全的,那么其实怎么实现的呢?

其实Vector的实现很简单,就是在每一个可能发生线程安全的方法上加synchronize关键字。这样就是的任何时候只有一个线程能够进行读写,这样就保证了线程安全。

public synchronized E get(int index) {
    if (index >= elementCount)
        throw new ArrayIndexOutOfBoundsException(index);

    return elementData(index);
}
public synchronized E set(int index, E element) {
        if (index >= elementCount)
            throw new ArrayIndexOutOfBoundsException(index);

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

扩容大小

与ArrayList类似,Vector在插入元素时也会检查容量并扩容。在Vector中这个方法是:ensureCapacityHelper

private void ensureCapacityHelper(int minCapacity) {
    // overflow-conscious code
    if (minCapacity - elementData.length > 0)
        grow(minCapacity);
}
private void grow(int minCapacity) {
        // overflow-conscious code
        int oldCapacity = elementData.length;
        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);
    }

其实上述扩容的思路与ArrayList是相同,唯一的区别是Vector的扩容大小。

int newCapacity = oldCapacity + ((capacityIncrement > 0) ?
                                 capacityIncrement : oldCapacity);

从上面的代码可以看到:如果capacityIncrement大于0,那么就按照capacityIncrement去扩容,否则扩大为原来的2倍。而ArrayList则是扩大为原来的1.5倍。

总结:

Vector与ArrayList在实现上是完全一致的,但是它们在某些方法有些许不同:

  • 第一,Vector是线程安全,而ArrayList是线程不安全的。Vector直接使用synchronize关键字实现同步。
  • 第二,Vector默认扩容为原来的2倍,而ArrayList默认扩容为原来的1.5倍。

更多资料,公号《Java路》