JAVA common collection summary ArrayList

ArrayList source code analysis expansion process and performance optimization

ArrayList default initialization process, an empty list with an initial capacity of 10
	 //首先我们看源码,无参构造函数
     /**
     * Constructs an empty list with an initial capacity of ten.
     */
    public ArrayList() {
        this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
    }
    
    
    //DEFAULTCAPACITY_EMPTY_ELEMENTDATA 的初始化
    /**
     * Shared empty array instance used for default sized empty instances. We
     * distinguish this from EMPTY_ELEMENTDATA to know how much to inflate when
     * first element is added.
     */
    private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};
     
     //可以再看一下add方法中,调用的ensureCapacityInternal方法中判断elementData == EMPTY_ELEMENTDATA 是否为空,如果为空,minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity);,minCapacity设置为其中的大值,在默认是空的时候,初次调用add方法传入的参数是1,也就是这里的minCapacity就是1,现在minCapcity变成了DEFAULT_CAPACITY 也就是10
     public boolean add(E e) {
        ensureCapacityInternal(size + 1);  // Increments modCount!!
        elementData[size++] = e;
        return true;
    }
     private void ensureCapacityInternal(int minCapacity) {
        if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {
            minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity);
        }

        ensureExplicitCapacity(minCapacity);
    }
     
     //默认值
     /**
     * Default initial capacity.
     */
    private static final int DEFAULT_CAPACITY = 10;

The expansion rule of ArrayList

通过下面的源码发现,如果空间不够,会通过Arrays.copyOf()创建一个新的内存空间,新空间的大小最小为原空间的1.5倍,这里是通过位移运算,效果相当于 
oldCapacity/2 = oldCapacity >> 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
     */
    private void grow(int minCapacity) {
        // overflow-conscious code
        int oldCapacity = elementData.length;
        int newCapacity = oldCapacity + (oldCapacity >> 1);
        if (newCapacity - minCapacity < 0)
            newCapacity = minCapacity;
        if (newCapacity - MAX_ARRAY_SIZE > 0)
            newCapacity = hugeCapacity(minCapacity);
        // minCapacity is usually close to size, so this is a win:
        elementData = Arrays.copyOf(elementData, newCapacity);
    }

As can be seen from the above source code, ArrayList is very wasteful of memory. When the collection is too large, OOM is easy to occur. Then when you know the length range of List, bring the length new ArrayList(256) when instantiating ArrayList, To reduce memory fragmentation and memory copy times. When a large amount of text content or Excel is imported into a scene, you can consider segmenting it first instead of importing it into memory at one time

Vector

The initialization size of the vector is also 10, which is different from the ArrayList in that the dynamic strategy is different, and the synchronized keyword is added to determine the critical section of the method and synchronize the threads.

    public Vector() {
        this(10);
    }
 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);
    }
public synchronized void setSize(int newSize) {
        modCount++;
        if (newSize > elementCount) {
            ensureCapacityHelper(newSize);
        } else {
            for (int i = newSize ; i < elementCount ; i++) {
                elementData[i] = null;
            }
        }
        elementCount = newSize;
    }

LinkedList

The data structure of LinkedList is a doubly linked list. The advantages of a doubly linked list and an array are that traversing and deleting elements is better than an ArrayList of dynamic arrays.

A blog post that says java collections well

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325986966&siteId=291194637