ArrayList vs Vector

ArrayList and Vector collections are used interchangeably for storing several items with the same type inside the memory.

In this tutorial, we list the major similarities and differences between these 2 collections.

1- Similarities

  • Both collections implement List interface, hence they expose similar operations for add, edit and delete operations.
  • Both store their items in a dynamic Array inside the memory.
  • Both maintain the insertion order of their elements, i.e. elements are retrieved with the same order of their insertion.
  • Both allow nulls and duplicate elements.

2- Differences

Here below we list the major differences between ArrayList and Vector:

  • Vector is implicitly synchronized, it works perfectly with multi-threaded environments. Upon each access, the thread acquires a lock on the Vector hence preventing any other thread from modifying it simultaneously. In contrast, ArrayList is not synchronized by default, so multiple threads can access and modify it at the same time. In order to achieve synchronization, a developer should explicitly surround each modification on ArrayList with a synchronized block OR convert it to a synchronized list using Collections.synchronizedList.
  • ArrayList is faster than Vector due to the fact that Vector implicitly checks for synchronization on each method call even in a single threaded environment.
  • Vector and ArrayList use a dynamic Array for storing their elements. When this array becomes out of room, ArrayList increases the size of array to 50% of the current size, while Vector increases it to 100%. So clearly Vector consumes more memory than ArrayList.

In short, Vector is deprecated and it doesn’t offer any added value over a synchronized ArrayList, so it’s always recommended to prevent vectors especially in new projects.

 

Hussein Terek

Founder of programmergate.com, I have a passion in software engineering and everything related to java environment.

You may also like...

Leave a Reply

avatar