Differences and comparisons between Vector and ArrayList in Java

  • 2020-04-01 03:47:31
  • OfStack

Vector  And   ArrayList comparison

Sometimes Vector is better, sometimes ArrayList is better, it is not easy to give the answer accurately, because it depends on the specific situation, there are four main factors to consider:

1: the API
2: synchronization
3: data growth (Data  Growth)
4: usage mode (Usage  The pattern)

The following meanings are discussed:

1: the API

In the description of the Java programming language by Ken Arnold, James Gosling, and David Holmes, Vector and ArrayList are designed by analogy. From the point of view of the API, there are many similarities between the two classes, but there are also some differences between the two classes.

2: Synchronization

From the perspective of synchronization:   Vector is synchronous, some methods to access the contents of the Vector are thread-safe, while ArrayList is asynchronous, methods to access the contents of the ArrayList are thread-unsafe, because of this difference, using the synchronized keyword modifies performance, so if you don't need a thread-safe collection, use ArrayList. There is no need to spend unnecessary synchronization performance overhead.

3: data growth (Data    Growth)

From the perspective of the internal data structure, both the ArrayList and the Vector use  ; Array mode holds the contents, you need to be careful in programming this nature, when inserting data into ArrayList or Vector, if the object storage of ArrayList or Vector exceeds the corresponding space (that is, the internal Array length). The corresponding ArrayList or Vector will expand their inner array. One times, the default expansion of internal Vector array is equivalent to two before the size of the array, and the size of the ArrayList only increased by 50%, according to which class you use to determine the influence of increasing the performance of one of the elements in the best way is to set the object's initial capacity for maximum capacity, it can avoid later after inserting elements from growth, (since growth will involve an array of internal element is copied to the newly created in the array), if you don't know how much data will grow to, but you know the rate at which data growth, Vectory can have a slight advantage, Because you can set the values to increase.

As for why to set the Vector to be synchronous and self-growing to double its size and the ArrayList to be asynchronous self-growing to half its size:

Gossip: setting to sync indicates that the data growth is quite intense, that is, the growth rate and frequency are large, if set to half, soon the array is full again, so set to double.

The corresponding growth rate of ArrayList is slow, and it is set to be out of sync, so it is not easy to have the problem of multi-threading.
Gossip is just personal understanding, welcome to clap brick.

4: usage pattern

ArrayList and Vector are better in retrieval of elements in a specific location, and at the end of the collection of add and remove elements, all of the above operations are in linear time of O (1), but if you add and remove some other position of the element (not at the end of the location) need to be more expensive linear time overhead is O (n - I), n represents the number of total elements I represents the insert or remove the index of the position, the operation cost more, because have to move the index I back-end all elements, what does it mean?

This means that you first find the element with index of and after I, and then insert and remove that element at the end of the array. If you want to do insert or delete, can consider to some other collection classes, such as: LinkedList able to add or remove some position of the element in constant time O (1), but the location of the index elements is slow, need time to O (I) overhead, I need is the index of the element, traverse the ArrayList is easy, because you can simply use the index instead of creating an iterator, LinkedList also for each insert element to create an internal object, so you must be aware that additional garbage is created.

One last caveat: you can use the most common array instead of a Vector or an ArrayList, especially because of the performance of standard code, using an array avoids synchronization, extra method calls, and the most modest resize, and you only need to spend extra development time. Array enables you to tailor collections to your program.


Related articles: