Vector vs ArrayList in Java
ArrayList and Vector are the two most commonly used classes in the package collection of java and the difference between Vector and ArrayList is a very frequently asked question. While this is a simple question, it's important to know when to use Vector or ArrayList, especially when you're working on a large project.In this article, we'll look at the points of difference between a Vector and an ArrayList in Java and try to understand the concept behind those differences.
Before we get into the differences between Vector and Vector and ArrayList, let's see the similarities between these two:
- Vector and ArrayList use an extensible storage structure.
- ArrayList and Vector use an internal array structure.
- Both lists maintain the order in which elements are inserted. This means that you can retrieve the object in the order in which it was inserted if you browse ArrayList or Vector.
- Vector and ArrayList allow duplicate elements, and null values.
- Iterator and listIterator returned by Vector and ArrayList are of type fail-fast.
Vector vs ArrayList
1) Synchronization
ArrayList is not synchronized, which means that multiple threads can access it at the same time. For example, if a thread is performing an append operation in ArrayList, another thread may be performing a parallel delete operation in an unsynchronized multi-threaded environment.
Vector is synchronized. This ensures secure use of threads in a concurrent and multi-threaded environment. For example, if a thread is performing an operation in Vector, no other thread can access it until the first thread has finished. Unlike ArrayList, only one thread can perform an operation at a time.
2) Performance
ArrayList is more performant and faster because it is unsynchronized, making it a very good choice in a single-threaded environment. You can also use ArrayList in a multi-threaded environment if the threads are only reading the values of ArrayList.
Since Vector is synchronized, it pays the price for synchronization, which makes it slow and performs poorly. The thread that performs an operation blocks access to the other threads therefore, they wait until the lock is released.
Since Vector is synchronized, it pays the price for synchronization, which makes it slow and performs poorly. The thread that performs an operation blocks access to the other threads therefore, they wait until the lock is released.
3) Resizing
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-fast
First, 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.
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:
4) fail-fast
First, 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 |