Difference Between ArrayList and Vector (With Table)

ArrayList and vectors store elements of the array. They allow the users to store multiple objects. Data is stored dynamically in both of them. Array list and vector run on java. util package. The treatment of operations like the addition of elements, subtraction of elements, iterating the elements, duplication, or null elements are quite similar. But it is important to know the areas where they perform differently.

ArrayList vs Vector

The difference between ArrayList and Vector lies in the pathway through which they store the data and process it. Both the methods let the users perform a series of functions. Programmers prefer to use ArrayList or vector depending upon their requirements. While one is synchronized, the other is non-synchronized. Their expandable capacity varies. They let the user perform from simplest to more complex operations.

ArrayList helps the user to make modifications in the size of the array. ArrayList makes the array shrink or expand based on the user’s requirement. It is different from built-in arrays as built-in arrays do not let the user modify the size of the array. ArrayList could operates on multiple thread synchronously.

Vector is found in java. util package. It supports a dynamic array of elements which means the array is resizable. Vectors belong to the legacy class. Vectors perform thread-safe operations which means a single thread can perform a single operation at a time which tends to make their performance slower. 

Comparison Table Between ArrayList And Vector

Parameters of ComparisonArrayListVector
SynchronizationArrayList is not Synchronized i.e., it could work on various threads simultaneously.Vector is synchronized i.e., only one thread could handle the code at a moment.
SpeedIts operations are fast as they are non-synchronized.Vector operations run slower as they are synchronized.
ResizingIf elements exceed their capacity then ArrayList increases 50% of the existing array size.If elements exceed their capacity then the vector increases 100% of the existing array size.
PreferenceProgrammers prefer ArrayList over vectors.It is less preferred as synchronization in vectors causes inferior performance.
Traversal(pass-through)It uses Iterator to traverse the elements.It can use the Iterator as well as enumeration to traverse the elements.

What is an ArrayList?

ArrayList is a modifiable array. It is found in java. util package. It has a different function than the built-in array. The creation of an ArrayList is helpful when the user does not know the size of the data to be included. ArrayList considers null or void values as valid.

ArrayList lets the users perform the basic operations of adding elements, removing elements, changing elements, and loop operation. 

ArrayList supports multiple operations. 

  1. To add elements: Use add() operation to add elements.
  2. To access any element: Elements can be accessed using the get() option. Elements are accessed using an indexer which starts from zero. 
  3. To change an element: To make changes in any specific element, use set() operation.
  4. To remove elements: These three operations Remove(), Removerange( , ) and RemoveAt are used to remove elements in the arraylist.

ArrayList is operated by its size. Though it can be expanded by adding the elements and shrank by removing the elements.

The following two methods are used to check whether an element exists in the Java ArrayList or not.

  1. contains()
  2. indexOf()

What is a Vector?

Vector is considered to be a legacy class that is synchronized. It supports one operation at a time. If multiple threads are needed to operate, then no two threads can perform simultaneously. This results in inferior performance as one thread need to wait while the other thread works on the vector.

Vectors are considered to be thread-safe. Thread safety ensures the users that all threads function appropriately and satisfy their design criteria without unwanted interaction. 

Vector ensures the addition or deletion of elements by expanding or shrinking the size of the vector. There may be some cases where the user does not have prior knowledge regarding the length of an array. In those cases, vectors give an edge to the users. Vector can expand 100% if the number of elements to be added exceeds its capacity. 

Vector just like ArrayList lets the users perform the basic operations of adding elements, removing elements, changing elements, and loop operation.

  1. To add elements: Use add() operation to add elements.
  2. To change elements: Use set() operation to change elements. Elements in vectors are attributed by their index.
  1. To remove elements: Use the remove() method to remove elements from the vector.

Vector supports other simplex operations apart from the basic stated operations.

Main Differences Between ArrayList And Vector

  1. ArrayList and vector both can be shrunk or expanded based on the number of elements that need to be included. But their resizability capacity differs. ArrayList can only expand by half the size of vectors.
  2. ArrayList gives the users superior performance as two different threads can perform their respective operations simultaneously. On the other hand, in vectors, only a single thread can operate at a particular time. If there are two threads in a vector then one thread has to wait while the other thread performs its operation.
  3. ArrayList is not synchronized by default. But users can choose to synchronize it by performing the required operation. Whereas vector operations are synchronized by default and cannot be altered.
  4. ArrayList operations are not thread-safe whereas vector operations are thread-safe.
  5. ArrayList is a collection class whereas vector is a legacy class.

Conclusion

ArrayList and vector both allow the users to perform multiple operations. ArrayList is considered to be better as it delivers superior performance in a shorter time frame. Vectors have a slight advantage when the users do not know the length of the data to be added in an array. Programmers can use any method based on their requirements.

Reference

  1. https://dl.acm.org/doi/abs/10.1145/1044550.1041666
  2. https://dl.acm.org/doi/abs/10.1145/3394451.3397204

x
2D vs 3D