What is the difference between ArrayList, list, and Vector?
1. The underlying layer of ArrayList is implemented using arrays (and the Array type is changed to Object type). 2. If jdk6 is used, Array is used. the of () method is used to generate a new array. If jdk5.0 is used, System is used. arraycopy method (copy an array)
3. List list = new ArrayList (); an array of 10 characters is generated at the underlying layer to store objects.
It is best to determine the minimum length of the array by using the new ArrayList (int length) method, for example, new
ArrayList (50) to improve the efficiency of the underlying layer.
4. For ArrayList and Vector, the underlying layer is implemented using arrays (this array is an Object-type array)
5. For ArrayList, all methods are not synchronized. For Vector, most public methods are synchronized.
6. The underlying layer of the sorted list is implemented by a bidirectional cyclic linked list. For ArrayList, the query speed is very fast, and the addition and deletion operations are very high.
Slow. (It is determined by the characteristics of the array)
7. For LinkList, the search speed is very slow, and the addition and deletion operations are very fast. (Essentially, it is determined by the characteristics of a two-way circular linked list.
)
I. Synchronization
ArrayList and inclulist are not synchronized, while Vestor is. Therefore, if thread security is required, ArrayList or javaslist can be used to save costs for synchronization. But in the case of multithreading, sometimes we have to use Vector. Of course, you can also package ArrayList and history list in some ways to synchronize them, but the efficiency may be reduced.
Ii. Data Growth
In terms of the internal implementation mechanism, both ArrayList and Vector are stored in the Objec array format. When you add elements to these two types, if the number of elements exceeds the current length of the internal array, both of them need to extend the length of the internal array, by default, Vector automatically doubles the length of the original array, and ArrayList is 50% of the original length. Therefore, the space occupied by this set is always larger than what you actually need. Therefore, if you want to save a large amount of data in the collection, using Vector has some advantages, because you can avoid unnecessary resource overhead by setting the initialization size of the collection.
Iii. Efficiency of searching, inserting, and deleting objects
In ArrayList and Vector, retrieving an object from the specified position (using index) or inserting or deleting an object at the end of the set takes the same time, can be expressed as O (1 ). However, if an element is added or removed from another position in the Set, the time consumed will grow linearly: O (n-I), where n represents the number of elements in the set, I indicates the index location where the element is added or removed. Why? When performing the preceding operations, all the elements after the I and I elements in the set must perform the (n-I) object displacement operation.
In the sorted list, the time spent on inserting or deleting elements at any position in the set is the same-O (1), but it is slow when indexing an element, is O (I), where I is the index location.
Therefore, if you only search for elements at a specific position or add or remove elements only at the end of the set, you can use Vector or ArrayList. If you want to insert or delete other operations at the specified position, you are advised to select the recommended list.