Comparison of java vector and ArrayList
Today I studied the source codes of Vector and ArrayList, and deepened my understanding of these two classes.
There are three classes implemented in the List interface: ArrayList, Vector, and LinkedList. I won’t say much about LinkedList, it is generally used to maintain the order of insertion of data.
ArrayList and Vector are both implemented using arrays, and there are three main differences:
1. Vector is multi-threaded and safe, while ArrayList is not. This can be seen from the source code. Many methods in the Vector class are modified by synchronized, which leads to the efficiency of Vector that cannot be compared with ArrayList;
2. Both use linear continuous space storage elements, but when the space is insufficient, the two classes are added differently. Many netizens say that Vector doubles the original space, and ArrayList increases the original space by 50%. In fact, this is about the same. However, there are still some problems that can be seen from the source code, and it will be analyzed from the source code later.
3. Vector can set the growth factor, but ArrayList cannot. When I first looked at this, I didn’t understand what the incremental factor was. However, I understood this by comparing the two source codes. Let’s first look at the construction methods of the two classes:
ArrayList has three construction methods:
public ArrayList(int initialCapacity)//Construct an empty list with the specified initial capacity. public ArrayList()//Construct an empty list with an initial capacity of 10. public ArrayList(Collection<? extends E> c)//Construct a list of elements containing the specified collection
Vector has four constructors:
public Vector()//Construct an empty vector using the specified initial capacity and capacity increment equal to zero. public Vector(int initialCapacity)//Construct an empty vector to make the size of its internal data array, and its standard capacity increment is zero. public Vector(Collection<? extends E> c)//Construct a vector containing elements in the specified collection public Vector(int initialCapacity,int capacityIncrement)//Construct an empty vector using the specified initial capacity and capacity increments
Vector has one more construction method than Arraylist. That's right, the construction method of public Vector (int initialCapacity, int capacityIncrement). capacityIncrement is capacity growth, which is the growth factor mentioned above, which is not available in ArrayList.
Then post two classes to add source code analysis (jdk1.7 version):
//ArrayList class add source code: public boolean add(E e) { ensureCapacityInternal(size + 1); //Increments modCount!! elementData[size++] = e; return true; } private void ensureCapacityInternal(int minCapacity) { modCount++; // overflow-conscious code //If after adding an element, the size of the new container is greater than the capacity of the container, then the value cannot be saved. The space needs to be expanded if (minCapacity - elementData.length > 0) grow(minCapacity); } private void grow(int minCapacity) { // overflow-conscious code int oldCapacity = elementData.length; int newCapacity = oldCapacity + (oldCapacity >> 1); //The expansion space increases by 50% (that is, 1.5 times the original) if (newCapacity - minCapacity < 0) //If the container is still not enough after expansion, then just set minCapacity to the size of the container newCapacity = minCapacity; if (newCapacity - MAX_ARRAY_SIZE > 0) //If the expanded container is too large, then execute hugeCapacity newCapacity = hugeCapacity(minCapacity); // minCapacity is usually close to size, so this is a win: elementData = Arrays.copyOf(elementData, newCapacity); } Add source code to the Vector class:
public synchronized boolean add(E e) { modCount++; ensureCapacityHelper(elementCount + 1); elementData[elementCount++] = e; return true; } 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); /** This capacity expansion requires a judgment: if the capacity increment is not initialized by 0, that is, the initialization of the public Vector(int initialCapacity, int capacityIncrement) constructor, then the capacity expansion capacity is (oldCapacity+capacityIncrement), which is the value of the original capacity plus the capacity increment; if the capacity increment is not set, then the capacity after expansion is (oldCapacity+oldCapacity), which is twice the original capacity. **/ if (newCapacity - minCapacity < 0) newCapacity = minCapacity; if (newCapacity - MAX_ARRAY_SIZE > 0) newCapacity = hugeCapacity(minCapacity); elementData = Arrays.copyOf(elementData, newCapacity); }Through analysis, it should be understandable now!
Thank you for reading, I hope it can help you. Thank you for your support for this site!