What is the Difference Between ArrayList and Vector

The main difference between ArrayList and Vector is that the ArrayList is non-synchronized and allows multiple threads to work on an ArrayList at the same time while the Vector is synchronized and avoids multiple threads working on a vector at the same time.

Programming languages such as Java supports collections, which allows storing multiple objects as a single unit. Two such collections are ArrayList and Vector. They help to store data dynamically. ArrayList is non-synchronized, which makes an ArrayList performs better than a Vector. On the other hand, vector is synchronized and contains many legacy methods.

Key Areas Covered

1. What is ArrayList
     – Definition, Functionality
2. What is Vector
     – Definition, Functionality
3. What is the Difference Between ArrayList and Vector
     – Comparison of Key Differences

Key Terms

ArrayList, Vector

Difference Between ArrayList and Vector -Comparison Summary

What is ArrayList

ArrayList is a data structure that is implemented using the ArrayListClass. This ArrayList class further implements the List interface. It is a better alternative for arrays. Usual arrays have a fixed length. Therefore, the programmer cannot add more elements than the defined array size. Using an ArrayList, it is possible to change the array size dynamically. In other words, the programmer can add or remove elements dynamically. Therefore, it is a flexible data structure. ArrayList maintains the element inserted order. Moreover, there are predefined methods available in ArrayList class. The programmer can use them in his program.

Main Difference - ArrayList vs Vector

Figure 1: ArrayList program

If you look at the above program, “fruits” is an object of type ArrayList. It can store strings. The add method helps to insert elements to the ArrayList. The element in the 2nd index of the ArrayList is grapes. The remove method helps to remove “grapes” from the ArrayList. Now there are only three elements available. The “itr” is an iterator. It helps to iterate through the ArrayList. Finally, the while loop prints the elements available in the ArrayList.

What is Vector

Vector is a data structure implemented using the Vector class. The Vector class implements the List interface. Vector maintains the element inserted order. It is synchronized. Therefore, multiple threads cannot work on a vector simultaneously. Usually, the performance of adding, deleting and updating elements in a vector is lower.

Difference Between ArrayList and Vector

Figure 2: Vector program

In the above program, “vec” is an object of Vector that can store strings. The add method helps to insert new elements into the vector. Similarly, the remove method helps to remove an element from the vector. Therefore, when passing “orange” to the remove method, that specific element is removed from the vector. The “en” is an enumeration, which helps to iterate through a vector.  Finally, the while loop prints the elements available in the vector.

Difference Between ArrayList and Vector

Definition

ArrayList is a non-synchronized data structure that uses a dynamic array for storing the elements while vector is a synchronized data structure that uses a dynamic array for storing the elements. Thus, this is the fundamental difference between ArrayList and Vector.

Performance

Performance is a major difference between ArrayList and Vector. An ArrayList works faster than a Vector. Therefore, its performance is higher than the vector.

Synchronization

Aboveall, synchronization is the main difference between ArrayList and Vector. An ArrayList is non-synchronized whereas vector is synchronized.

Traversing the Elements

While ArrayList uses Iterator interface to traverse the elements, vector uses the Iterator interface or the Enumeration interface to traverse the elements. Hence, this is another difference between ArrayList and Vector.

Array Size

Moreover, ArrayList increments 50% of current array size if the number of elements exceeds its capacity while vector increments 100%, i.e., doubles the array size if the total number of elements exceeds its capacity.

Conclusion

Both ArrayList and Vector allow storing elements dynamically. The main difference between ArrayList and Vector lies in synchronization. ArrayList is non-synchronized and allows multiple threads to work on it at the same time while Vector is synchronized and avoids multiple threads working on it at the same time. Thus, ArrayList is faster than vector.

Reference:

1. “ArrayList in Java – Javatpoint.” Www.javatpoint.com, Available here.
2. Singh, Chaitanya, and J Padilha. “Vector in Java.” Beginnersbook.com, 8 Aug. 2017, Available here.

About the Author: Lithmee

Lithmee holds a Bachelor of Science degree in Computer Systems Engineering and is reading for her Master’s degree in Computer Science. She is passionate about sharing her knowldge in the areas of programming, data science, and computer systems.

Leave a Reply