Explain the difference between Vector and ArrayList in Java

  • 2020-05-12 02:34:19
  • OfStack

First of all, both of these classes implement the List interface, and List interface 1 has three implementation classes, ArrayList, Vector, and LinkedList. List is used to store 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 is the most commonly used List implementation class, implemented internally through arrays, which allow 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 enough, you need to increase the storage capacity. You need to copy the data of an existing array into a new storage space. When inserting or deleting elements from the middle of 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. Like Vector and ArrayList1, Vector is also implemented through array. The difference is that it supports the synchronization of threads, that is, only one thread can write Vector at a certain time, avoiding the non-uniformity caused by simultaneous writing by multiple threads.

3.LinkedList USES the linked list structure to store data, which is very suitable for dynamic insertion and deletion of data, with slow random access and traversal speed. In addition, he provides methods that are not defined in the List interface, which are specialized for manipulating header and tail elements and 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.

ArrayList:


public boolean add(E e) {
 ensureCapacity(size + 1); //  Add an element to see if it can hold. If you can't, you have to 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; //  I don't see why this is useful. What are the developers thinking  
  int newCapacity = (oldCapacity * 3)/2 + 1; //  Increase 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:

ArrayList is extended 50% + 1 by default when out of memory, and Vector is extended 1 times by default.
Vector provides indexOf(obj, start) interface, ArrayList does not.
Vector is thread-safe, but Vector is not used in most cases because thread safety requires more overhead.


Related articles: