Vector is legacy class which is available with Java before collection framework comes into picture while ArrayList was introduce with collection framework. There are some basic differences between ArrayList and Vector as well as few similarities. It is very common to ask question around ArrayList and Vector in interviews. Also if we know the differences and similarities between ArrayList and Vector, it will be easier for us to select one of these while writing our code.
Differences between ArrayList and Vector.
|ArrayList object is not synchronized by default.||Vector object is synchronized by default.|
|In case of single thread, using ArrayList is faster than the Vector.||In case of multiple thread, using Vector is advisable. With a single thread, Vector become slow.|
|ArrayList increases its size every time to 50 percent.||Vector increases its size every time by doubling it.|
|ArrayList can use only Iterator to access the elements.||Vector list can use Iterator and Enumeration.|
Similarities between ArrayList and Vector.
- Both ArrayList and Vector are derived from AbstractList and implements List interface, which means both of them are ordered collection and allows duplicates.
- ArrayList and Vector are index based Collection and we can use get(index) method to retrieve objects.
Other Interview questions.