ArrayList vs Vector

In this Java tutorial, we will discuss the difference between ArrayList and Vector.

ArrayList and Vector both implements List interface and maintains insertion order and both use Array as a data structure internally.

However, there are few differences in the way they store and process the data. In this post, we will discuss the difference and similarities between ArrayList and Vector.

 

Let’s see the differences between ArrayList and vector class-

 

ArrayList vs Vector

1. Synchronization or Thread Safe :  First and foremost difference between Vector and ArrayList is that Vector is synchronized and ArrayList is non-synchronized in Java. Which means is that all the method which structurally modifies Vector e.g. add () or remove () are synchronized which makes it thread-safe and allows it to be used safely in a multi-threaded and concurrent environment. On the other hand, ArrayList methods are not synchronized thus not suitable for use in the multi-threaded environment.

i.e If one thread is performing an add operation on ArrayList, there can be an another thread performing remove operation on ArrayList at the same time in a multithreaded environment

while Vector is synchronized. This means if one thread is working on Vector, no other thread can get a hold of it. Unlike ArrayList, only one thread can perform an operation on the vector at a time.

 

2. Speed and Performance :  ArrayList is way faster than Vector. Since Vector is synchronized and thread-safe it pays a price of synchronization which makes it little slow. On the other hand, ArrayList is not synchronized and fast which makes it the obvious choice in a single-threaded access environment.

 

3. Data Growth :  Both ArrayList and Vector can grow and shrink dynamically to maintain the optimal use of storage, however, the way they resized is different. ArrayList grows by half of its size when resized while Vector doubles the size of itself by default when grows.

4. Enumeration and Iterator : Vector can return the enumeration of items it holds by calling elements () method which is not fail-fast as opposed to Iterator and ListIterator returned by ArrayList.

 

5. Fail-Fast : First let’s understand, what is fail-fast: If the collection (ArrayList, vector etc) gets structurally modified by any means, except the add or remove methods of an iterator, after the creation of iterator then the iterator will throw ConcurrentModificationException. Structural modification refers to the addition or deletion of elements from the collection.

 

As per the Vector Javadoc, the Enumeration returned by Vector is not fail-fast. On the other side, the iterator and listIterator returned by ArrayList are fail-fast.

 

Similarities in ArrayList and Vector

  • Vector & ArrayList both allows duplicate and null values.
  • They both grows and shrinks automatically when overflow and deletion happens.
  • They both are ordered collection classes as they maintain the elements insertion order.

 

Selenium Tutorial:

If you liked this video, then please subscribe to our YouTube Channel for more video tutorials.

0 Comment

Leave a Reply