The difference between a Vector and an ArrayList in Java

  • 2020-04-01 02:00:48
  • OfStack

Let's start with the fact that both classes implement the List interface, and the List interface has three implementation classes: ArrayList, Vector, and LinkedList. A List is used to hold multiple elements, to maintain the order of elements, and to allow the repetition of elements.
The relevant differences of the three concrete implementation classes are as follows:

1.ArrayList, the most commonly used List implementation class, is implemented internally through an array, which allows quick random access to elements. The disadvantage of arrays is that there can be no space between each element. When the array size is not satisfied, the storage capacity needs to be increased. When inserting or deleting elements from the middle of an ArrayList, the array needs to be copied, moved, and expensive. Therefore, it is suitable for random lookup and traversal, not for insertion and deletion.

2.Vector, like ArrayList, is also implemented through an array. The difference is that it supports synchronization of threads.

3.LinkedList is a LinkedList structure to store data, which is suitable for dynamic insertion and deletion of data, and the speed of random access and traversal is relatively slow. In addition, he provides undefined methods in the List interface for manipulating header and footer-table elements that can be used as stacks, queues, and bidirectional queues.

Looking at the Java source code, you can see that when the size of the array is insufficient, you need to rebuild the array and then copy the elements into the new array. The size of the ArrayList and Vector extension arrays are different.
In the ArrayList:

public boolean add(E e) {
     ensureCapacity(size + 1);  //Add an element to see if it can hold. If you can't, create a new array
     elementData[size++] = e;
     return true;
 }
  public void ensureCapacity(int minCapacity) {
     modCount++; 
     int oldCapacity = elementData.length;
     if (minCapacity > oldCapacity) {
         Object oldData[] = elementData; //This line doesn't seem useful. What are the developers thinking
         int newCapacity = (oldCapacity * 3)/2 + 1; //Increases the size of the new array
         if (newCapacity < minCapacity)
        newCapacity = minCapacity;
             // minCapacity is usually close to size, so this is a win:
             elementData = Arrays.copyOf(elementData, newCapacity);
     }
 }

Vector:

private void ensureCapacityHelper(int minCapacity) {
     int oldCapacity = elementData.length;
     if (minCapacity > oldCapacity) {
         Object[] oldData = elementData;
         int newCapacity = (capacityIncrement > 0) ?
        (oldCapacity + capacityIncrement) : (oldCapacity * 2);
         if (newCapacity < minCapacity) {
        newCapacity = minCapacity;
         }
        elementData = Arrays.copyOf(elementData, newCapacity);
     }
 }

The differences between ArrayList and Vector are as follows:
1. When ArrayList is out of memory, the default is to extend 50% + 1, and the Vector is 1 times of the default.
2. Vector provides indexOf (obj, start) interface, while ArrayList does not.
3.Vector is thread-safe, but is not used most of the time because thread safety requires more overhead.

Related articles: