java details set (a): Please specify a set of initial capacity

Our collection is very widely used, it is like the sea in Java programming, be tolerant to diversity, like universal container, dressed all things, and the sea, universal container can become infinitely large (if conditions allow). When the amount of the sea, the vessel becomes very large, its initial capacity will be very very important, because digging the sea, the expansion is the need to consume a large amount of manpower and financial resources. By the same token, the initial capacity of the Collection is also extremely important. So: For known scenario, specify the initial capacity of the collection.

public static void main(String[] args) {
    StudentVO student = null;
    long begin1 = System.currentTimeMillis();
    List<StudentVO> list1 = new ArrayList<>();
    for (int i = 0; i < 1000000; i++) {
        student = new StudentVO(i, "chenssy_" + i, i);
        list1.add(student);
    }
    long end1 = System.currentTimeMillis();
    System.out.println("list1 time:" + (end1 - begin1));
 
    long begin2 = System.currentTimeMillis();
    List<StudentVO> list2 = new ArrayList<>(1000000);
    for (int i = 0; i < 1000000; i++) {
        student = new StudentVO(i, "chenssy_" + i, i);
        list2.add(student);
    }
    long end2 = System.currentTimeMillis();
    System.out.println("list2 time:" + (end2 - begin2));
}

The above two codes are inserted into the list of data 1,000,000, but there is no application list1 initial capacity, initial capacity and list2 1,000,000. That results are as follows:

list1 time:1638
list2 time:921

Operating results from the above we can see that the speed is about twice list1 list2's. LZ on the front mentioned, ArrayList expansion mechanism is resource consuming. We look at the ArrayList add method:

public boolean add(E e) {  
        ensureCapacity(size + 1);   
        elementData[size++] = e;  
        return true;  
    }  
 
    public void ensureCapacity(int minCapacity) {  
        modCount++;         //修改计数器
        int oldCapacity = elementData.length;    
        //当前需要的长度超过了数组长度,进行扩容处理
        if (minCapacity > oldCapacity) {  
            Object oldData[] = elementData;  
            //新的容量 = 旧容量 * 1.5 + 1
            int newCapacity = (oldCapacity * 3)/2 + 1;  
                if (newCapacity < minCapacity)  
                    newCapacity = minCapacity;  
          //数组拷贝,生成新的数组
          elementData = Arrays.copyOf(elementData, newCapacity);  
        }  
    }

ArrayList Each time a new element, it will detect whether the current capacity of the ArrayList has reached a critical point, if you reach the critical point will be 1.5 times the expansion. However, expansion and copying of the arrays generate a new ArrayList array is very resource intensive. So if we had known usage scenarios collections, we understand that the range of the collection, the best we specified initial capacity, this use of resources will be even better, especially under the premise of a large amount of data and the use of resources and improve the efficiency of It will be even more advantageous.

Published 157 original articles · won praise 43 · views 90000 +

Guess you like

Origin blog.csdn.net/qq_39581763/article/details/104200119