Difference between ArrayList and Vector in java

One of the common interview question is “What is difference between ArrayList and Vector”.Before we actually see differences,let me give you brief introduction of both.

ArrayList

  • ArrayList is implementation of list interface.
  • ArrayList is not synchonized(so not thread safe)
  • ArrayList is implemented using array as internal data structure.It can be dynamically resized .
  • ArrayList increases half of its size when its size is increased.

Vector

  • Vector is implementation of list interface.
  • Vector is synchonized(so thread safe)
  • Vector is implemented using array as internal data structure.It can be dynamically resized.
  • Vector doubles size of array when its size is increased.

ArrayList vs Vector:

Parameter
Vector
ArrayList
Synchonized
Yes
No
ThreadSafe
Yes
No
Performance
It is slower than arraylist
It is faster than Vector
Changes in internal  size of array when  resized
Vector doubles size of its internal  array when its size is increased
ArrayList increases half of its size when its size is increased.

Which is better? ArrayList or Vector?

It actually depends on our need.Vector is slower than ArrayList as its methods are synchronized so if we don’t work in multi threaded environment then ArrayList is better choice.

Best Practice:

When we initialize ArrayList or Vector,always initialize with largest capacity java program will need as incrementing size is costlier operation.

Can we synchronize ArrayList?

Yes,ArrayList can also be synchonized with help of method Collections.synchronizedList(arraylist)

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *