Wednesday, January 11, 2017

Java Difference between ArrayList and Vector

This question is mostly used as a start up question by Interviewers testing deep roots of the Collection. Some differences between ArrayList and Vector.





1. Synschronization and Thread-safe

Vector is synchronized while ArrayList is not synchronized. Syncrhonization and thread safe means at a time only one thread can access the code. In Vector class all the methods are synchronized.

2. Performance

Vector is slow as it is thread safe. In comparison ArrayList is fast as it is non synchronized. 

3. Automatic Increase in Capacity

A Vector defaults to doubling size of its array. While when you insert an element into the ArrayList, it increases its Array size by 50%.

4. Set Increment Size

Array List does not define the increment size. Vector defines the increment size.
public synchronized void setSize(int i) {}

5. Enumerator

Other than Hashtable, Vector is the only other class which uses both Enumeration and Iterator. While ArrayList can only use Iterator.


Share:

0 comments:

Post a Comment

Total Pageviews