Difference between ArrayList and Vector in Java

Java ArrayList and Vector both implement the List interface, preserving the order of insertion. However, there exist certain distinctions between ArrayList and Vector.

  1. Thread Safety (Synchronization)
  2. Data growth
  3. Performance
  4. Traversing (Iterator)
  5. Legacy

Thread Safety (synchronized)

By default, the Vector class in Java is synchronized, meaning that it ensures thread-safe operations on its elements, while the ArrayList class is not synchronized. Synchronization refers to the coordination and control of concurrent access to shared resources in a multi-threaded environment.

In the case of Vector, the synchronized nature ensures that multiple threads can safely access and modify the Vector's elements without encountering data corruption or inconsistencies. This synchronization comes with a performance cost, as acquiring and releasing locks for synchronization can introduce overhead.

On the other hand, ArrayList is not synchronized, which means that it does not provide inherent thread-safety. When multiple threads concurrently access or modify an ArrayList without proper synchronization mechanisms, it can lead to unpredictable results, such as data races or inconsistent state.

The choice between Vector and ArrayList depends on the specific requirements of the application. If thread-safety is a concern and there are multiple threads accessing or modifying the list concurrently, Vector can be a suitable choice. On the other hand, if thread-safety is not a requirement or can be managed externally, ArrayList may offer better performance due to its lack of synchronization overhead.

Data growth

Both ArrayList and Vector internally use an array to store their elements, allowing for dynamic resizing to efficiently utilize storage. However, the mechanism by which they resize differs.

ArrayList is initially created with a default size of 10. When the number of elements surpasses this initial capacity, the collection automatically increases its size by 50% of the current capacity, resulting in a new size of 15. This incremental growth strategy optimizes memory usage while accommodating additional elements.

In contrast, Vector follows a different resizing approach. It doubles the array size when the number of elements exceeds its current capacity. For example, if the capacity is initially set to 10 and the collection surpasses this limit, Vector will double its capacity to 20. This doubling mechanism allows for efficient allocation of memory when the collection grows.

Performance

ArrayList generally offers better performance compared to Vector due to its non-synchronized nature. Operations on Vector suffer from poorer performance because they are thread-safe, meaning that a thread working on Vector acquires a lock, causing other threads to wait until the lock is released. In contrast, ArrayList allows two or more threads to access the code simultaneously, while Vector limits access to one thread at a time.

Traversing (Iterator)

Elements in an ArrayList can be traversed using various techniques such as Iterator, ListIterator, and both regular and enhanced for loops. On the other hand, Vector utilizes the Enumeration interface for traversing its elements.

With ArrayList, you have the flexibility to use an Iterator or a ListIterator to iterate over the elements. The Iterator allows sequential access to the elements and supports operations like removal. The ListIterator provides additional features like bidirectional traversal and modification of elements. Alternatively, you can use a regular for loop or an enhanced for loop (also known as a foreach loop) to iterate through the ArrayList.

In the case of Vector, the Enumeration interface is employed for traversing the elements. Enumeration provides a legacy approach for iterating over the elements in a collection. It allows sequential access to the elements but lacks the advanced functionalities provided by Iterator or ListIterator.

Legacy

The Vector class was not originally a part of the Java Collections Framework; it was introduced later. As a result, it can be considered legacy code. The functionalities provided by Vector can also be accomplished using the List collection. Consequently, it is advisable to avoid using Vector in modern Java programming.

ArrayList Implementation

import java.util.*; class TestClass { public static void main (String[] args) { // create an array list Object ArrayList aList = new ArrayList(); aList.add("Sunday"); //adding item aList.add("Monday"); aList.add("Tuesday"); Iterator ir=aList.iterator(); while(ir.hasNext()){ System.out.println(ir.next()); } } //Output: Sunday Monday Tuesday

Vector Implementation

import java.util.*; class TestClass { public static void main (String[] args) { //create new Vector Object Vector vcTr = new Vector(); System.out.println("Vector Size: " + vcTr.size()); // initial size is 2, increment is 2 vcTr = new Vector(2,2); System.out.println("Vector Size: " + vcTr.size()); vcTr.addElement("Sunday"); vcTr.addElement("Monday"); vcTr.addElement("Wednesday"); System.out.println("Vector Size: " + vcTr.size()); System.out.println("Vector Capacity: " + vcTr.capacity()); //you can see here the capacity doubled as 4 //using Iterator to see all elemnets in vector Iterator < String > itr = vcTr.iterator(); while(itr.hasNext()){ System.out.println(itr.next()); } } }
Output
Vector Size: 0 Vector Size: 0 Vector Size: 3 Vector Capacity: 4 Sunday Monday Wednesday

Conclusion

The main differences between ArrayList and Vector in Java are that Vector is synchronized by default, while ArrayList is not, and ArrayList is generally more efficient in terms of performance and memory usage.