The storage capacity of both lists is extensible or contractile, Java designers have made it dynamic to maintain optimal storage usage. However, each has its own method. ArrayList grows by half its size while Vector doubles its default size. The capacity of ArrayList can be increased by calling the method
ensureCapacity().
4) fail-fastFirst, let's understand what a fail-fast is: if the collection (ArrayList, Vector, etc.) is modified except for the iterator add and remove operations, after iterator is created then iterator will raise an exception
ConcurrentModificationException. Changing the structure is related to adding or removing items from the collection.
Vector returns an Enumeration object by calling the
elements() that is not fail. On the other hand, Iterator and ListIterator returned by ArrayList are fail-fast.
5) Who really belongs to the collection framework?Vector is one of those classes that arrived with JDK 1.0 and initially was not part of the Collection framework but, in the latest version it has been taken into account because it implements the List interface, so, it became part of the collection.
ArrayList framework was introduced after Vector with the release of JDK 1.2. ArrayList was more advanced than Vector but it also had all the specifications of Vector. So, people started using ArrayList instead of Vector, and in this case, Vector became a legacy class.
When to use Vector and ArrayList?
It depends on your needs, if your code does the synchronization of the processes (threads), Vector would be the best choice since it ensures that only one thread accesses the collection at a time.
The Synchronization operations consume a lot of time compared to non-synchronized operations so, if your application needs to run faster and doesn't need synchronization, ArrayList would be the right choice, performance-wise.
This table summarizes the 5 differences we've seen:
ArrayList | Vector |
1) ArrayList is not synchronized. | Vector is synchronized |
2) ArrayList increments 50% of its current size if the number of elements exceeds its capacity | Vector invrementes 100%, this means double its original size if the number of elements exceeds its capacity |
3) ArrayList has been introduced in JDK 1.2. | Vector was introduced in JDK 1.0 |
4) ArrayList is fast because it is not synchronized | Vector is slow because it is synchronized. In a competitive environment, it will block other processes from accessing until the first thread allows access |
5) ArrayList uses Iterator to iterate through elements | Vector uses the Enumeration interface to iterate through elements, but, it can use Iterator too |