What is the Difference Between Arraylist and Vector?

🆚 Go to Comparative Table 🆚

The main differences between ArrayList and Vector in Java are:

  1. Synchronization: ArrayList is not synchronized, meaning multiple threads can access it at the same time. On the other hand, Vector is synchronized, allowing only one thread to access its methods at a time, which is more suitable for multithreading environments.
  2. Growth Strategy: When the number of elements exceeds the capacity of an ArrayList, it increments its current array size by 50%. In contrast, a Vector increments its current array size by 100%.
  3. Traversal: Vector can use both Enumeration and Iterator for traversing its elements, while ArrayList can only use Iterator for traversal.
  4. Performance: ArrayList generally has higher performance compared to Vector.

In summary, ArrayList is preferred over Vector in most single-threaded scenarios due to its higher performance and faster traversal using Iterator. However, when dealing with multithreading, Vector is often more suitable due to its built-in synchronization and more cautious growth strategy.

Comparative Table: Arraylist vs Vector

Here is a table summarizing the differences between ArrayList and Vector:

Feature ArrayList Vector
Synchronization Not synchronized Synchronized
Capacity increment Increments by 50% of the current array size if the number of elements exceeds its capacity Increments by 100% (doubles the array size) if the total number of elements exceeds its capacity
Performance Fast (due to non-synchronization) Slower (due to synchronization)
Multi-threading compatibility Allows multiple threads to work on ArrayList at the same time Only one thread at a time can access the code
Legacy class Not a legacy class; introduced in JDK 1.2 Legacy class

ArrayList and Vector are both implementations of the List interface in Java, but they have some significant differences. ArrayList is not synchronized, making it faster and allowing multiple threads to work on it simultaneously. On the other hand, Vector is synchronized, which means only one thread can access the code at a time, making it slower and less suitable for multi-threading environments. Also, when the capacity of the collection is exceeded, ArrayList increments its size by 50% of the current array size, while Vector increments its size by 100%.