First of all, we look at both categories that implement List interfaces, and there are three implementation classes in List interface, namely ArrayList, Vector and LinkedList. List is used to store multiple elements, can maintain the order of elements and allow repetition of elements.
The relevant differences between the three specific implementation classes are as follows:
1. ArrayList is the most commonly used List implementation class, internally implemented through arrays, which allows fast random access to elements. The disadvantage of arrays is that there cannot be spaced between each element. When the array size is not satisfied, the storage capacity needs to be increased. It is necessary to say that the data of the already-array is copied to the new storage space. When inserting or deleting elements from the middle position of the ArrayList, the array needs to be copied, moved, and the cost is relatively high. Therefore, it is suitable for random lookups and traversals, not for insertion and deletion.
2.Vector is also implemented through arrays, the difference is that it supports thread synchronization, that is, at a certain moment, only one thread can write a Vector to avoid inconsistency caused by multiple threads writing at the same time, but it costs a lot to implement synchronization, so accessing it is slower than accessing ArrayList.
3. LinkedList uses a linked list structure to store data, which is very suitable for dynamic insertion and deletion of data, and the random access and traversal speeds are relatively slow. In addition, it also provides methods that are not defined in the List interface, which are specifically used to operate table header and tail elements, and can be used as stacks, queues and bidirectional queues.
Looking at the Java source code, I found that when the size of the array is not enough, I need to re-create the array and then copy the elements into the new array. The size of the extended array of ArrayList and Vector is different.
ArrayList:
public boolean add(E e) { ensureCapacity(size + 1); // Add elements to determine whether they can accommodate them. 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; // This line did not show the usefulness, I don't know what the developers are thinking about int newCapacity = (oldCapacity * 3)/2 + 1; // Add 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); }}In 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 difference between ArrayList and Vector is as follows:
ArrayList is expanded by 50% + 1 by default when there is insufficient memory, and Vector is expanded by 1 times by default.
Vector provides indexOf(obj, start) interface, but ArrayList does not.
Vectors are at the thread safety level, but in most cases, vectors are not used because thread safety requires greater system overhead.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.