ArrayList vs Vector - other benefits than thread safety and performance?

Possible duplicate:
What are the differences between ArrayList and Vector?

If I want to store some objects in a data collection, and I have to choose between ArrayList or Vector, what are the main differences? I think the vector is thread safe and therefore has a performance penalty. Are there any other reasons for preferring data containers?

+4
source share
3 answers

A vector is a very old class before the Collections framework, so its API is polluted with a lot of inherited methods that duplicate methods from the Collection and List interface.

I would not use it at all if you do not need it, because the other API you are using requires it.

+3
source

In short, the main difference between Vector and ArrayList is that Vector is synchronized, and ArrayList is not. Thus, if several threads access the ArrayList at the same time, we must externally synchronize the code block, which modifies the list either structurally or simply modifies the element. Structural modification means adding or removing elements (elements) from the list. Setting the value of an existing item is not a structural modification. These two URLs will be helpful to you. http://javarevisited.blogspot.com/2011/09/difference-vector-vs-arraylist-in-java.html and http://geekexplains.blogspot.com/2008/05/difference-between-vector-and- arraylist.html

+3
source

You said it yourself. All methods in Vector are painfully synchronized. Collection synchronization, if required, can be applied outside the class, preferably in each case.

Another subtle difference between ArrayList and Vector is that you can control how the vector can grow. If in Arraylist the size of the internal array is always doubled

+2
source

Source: https://habr.com/ru/post/1385987/


All Articles