Vector vs ArrayList

Although there seems to be no difference between these two classes when they are used, they are both inherited from List and have the same methods, but their internals are somewhat different.   

a)  First of all, some methods in Vector are thread synchronized (synchronized) . The price of synchronization is reduced execution efficiency, but improved security. The ArrayList is thread asynchronous, and it can be read and written concurrently by multiple threads. If the thread safety factor is not considered, it is generally more efficient to use arraylist   

 

b) Internal data growth rate. All of these vector collections are internally stored and manipulated with arrays of Objects . So I understand why it can accept any type of Object , but it needs to be re-typed when it is taken out. Vector and ArrayList have the function of automatic scaling, we don't care how big it is , we can append elements after it. The growth rate of the internal array of Vector and ArrayList is different. When the internal array cannot hold more elements, Vector doubles the original value, and ArrayList increases the original 0.5 times.        

If you use a large amount of data in a collection, using a vector has certain advantages

 

List interface has a total of three implementation classes, 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 concrete implementation classes are as follows:

  1. ArrayList is the most commonly used List implementation class. Internally, it is implemented through an array, which allows fast random access to elements. The disadvantage of the array is that there can be no space between each element. When the size of the array is not sufficient, the storage capacity needs to be increased. It is necessary to copy the data of the existing array to the new storage space. When inserting or deleting elements from the middle 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 insertions and deletions.
  2. Like ArrayList, Vector is also implemented through arrays. The difference is that it supports thread synchronization, that is, only one thread can write Vector at a certain time, avoiding the inconsistency caused by multiple threads writing at the same time, but it requires a high cost to achieve synchronization. , so accessing it is slower than accessing an ArrayList.
  3. LinkedList uses a linked list structure to store data, which is very suitable for dynamic insertion and deletion of data, and the speed of random access and traversal is relatively slow. In addition, it also provides methods that are not defined in the List interface, which are specially used to operate the header and footer elements, which can be used as stacks, queues and bidirectional queues.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326522732&siteId=291194637