This is the seventh day of my participation in the August More text Challenge. For details, see: August More Text Challenge

ArrayList, Vector, and LinkedList are three common Collection classes that all implement the List interface (the List interface inherits from the Collection interface).

ArrayList; Vector; ArrayList

Why compare ArrayList and Vector in the first place? The most important thing is that they are both ordered collections, which means that the positions of the elements in both collections are ordered, and unlike arrays themselves, both are dynamic, so when we use both, we can retrieve an element by the positional index, and both allow the data to be repeated.

So what’s the main difference?

In terms of synchronicity

Vector is thread-safe, which means that multiple threads can access a shared resource with atomicity, visibility, and sequence. Vector thread safe way is to use the synchronized keyword, about this key principle, in my article “the noodles don’t panic when | is synchronized with the cognition is not the same as you have said, interested friends, welcome to browse.

In the case of ArrayList, it is thread-unsafe, and the entire implementation does not introduce any mechanism for locking shared resources.

So in multithreaded environments, Vector is recommended so that we don’t have to think about and write thread-safe code logic ourselves. In non-multithreaded environments, arrayLists are recommended to increase efficiency and reduce unnecessary locking costs.

In terms of data growth

Both ArrayList and Vector have an initial size. When the number of elements in the ArrayList and Vector exceeds the threshold, the storage space of each collection needs to be increased. Each time the storage space needs to be increased, considering the cost of expansion, the storage space needs to be increased not only by one storage unit, but by multiple storage units. The number of storage units increased each time must be balanced between memory space utilization and program efficiency.

If the number of stored elements exceeds the capacity of the current array, ArrayList will increase the size of the current array by 50% and Vector by 100%. That is, the ArrayList grows by 0.5 times and the Vector grows by one.

In terms of traversal

A Vector can use both Enumeration and Iterator to traverse elements, whereas an ArrayList can only use Iterator to traverse elements.

So with that said, how do we choose between an ArrayList and a Vector?

  • Vector is thread-safe, whereas ArrayList is not, so in general, a single thread uses ArrayListd and multiple threads use Vector.
  • If we don’t know how much data we’re going to have, but we know how fast the data is going to grow, Vector has an advantage because it can set the growth for expansion.
  • ArrayList is faster for updates, and is recommended if there are no special requirements.

It is important to note that Vector, as an early thread-safe dynamic arraylist, has many alternatives, such as

  • Collections.synchronizedList
  • CopyOnWriteArrayList

To be continued, tomorrow the overall analysis of the three differences!