VECTOR is still arraylist, which one is better, why?
To answer this question, it is not possible, sometimes it is better to use Vector; sometimes ArrayList, sometimes these two are not the best choice. Don't expect to get a simple and affirmative answer, because it depends on what you do. There are 4 factors to consider below:
(1) API
(2) Synchronous processing
(3) Data growth
(4) Use mode
The following is aimed at these four aspects.
The API has such a description in the books of "Java Programming Language" edited by Ken Arnold, and Vector is similar to arraylist.. All these two classes are very similar to all from the API perspective. But there is still some main differences between them.
Synchronization
Vector is synchronous. Some methods in this class ensure that objects in the VECTOR are threaded. ArrayList is asynchronous, so objects in ArrayList are not threads. Because synchronization requirements affect the efficiency of the execution, if you don't need a collection of thread security, use arraylist is a good choice, which avoids unnecessary performance overhead due to synchronization.
Data growth
From the internal implementation mechanism, ArrayList and Vector are used to control the objects in the collection (array). When you add an element to these two types, if the number of elements exceeds the current length of the internal array, they need to extend the length of the internal arrays, and Vector by default, the original double array length is automatically increased, and ArrayList is original. 50%, so the space you get this collection is always bigger than you actually needs. So if you want to save a lot of data in a collection, use the vector has some advantages, because you can avoid unnecessary resource overhead by setting the initialization size of the set.
Mode of use
In ArrayList and VECTOR, it is the same as the time to find data from a specified location (by index) or at the end of the collection, the time taken of the removal of an element is the same, this time we use O (1). However, if the time spent in addition to or removing the elements in the set of other locations, the time is linear: O (N-I), where n represents the number of elements in the collection, i represents an index position of the element to increase or remove elements. Why is this so? The operation of the displacement should be performed in all elements after the collections in the collection of the above operations. What does this mean?
This means that you just find the elements of a specific location or only add, remove the elements, so you can use the vector or arraylist. If you are other actions, you'd better choose another collection operation class. For example, the LinkList Collection class is the same - O (1) in the time of increasing or removing any position in any location in the collection, but it is slow -o (i) in the use of an element, where i is The location of the index. It is also easy to use ArrayList because you can simply use the index to replace the operation of creating the Iterator object. Linklist also creates an object for each inserted element, all you have to understand that it will also bring additional overhead.
Finally, in the "Practical Java" book, Peter Haggar recommends using a simple array (array) instead of Vector or ArrayList. Especially for programs with high execution efficiency requirements. Because the array is used to avoid synchronization, additional method calls and unnecessary reassignment operations.
PS: This article quoted from network About the author: Tony Sintes is a principal consultant of BroadVision. It is Sun's certification programmer and starts using Java development programs since 1997.