Last Updated: 15 January, 2023
ArrayList and Vector are both classes of the Collection Framework, and both classes implement the List interface.
Both the classes have some common features, such as maintaining the insertion order of elements and containing duplicate elements, etc.
However, there are many differences between the ArrayList and Vector classes that are given below.
ArrayList in Java is a concrete class of the List interface that was introduced in the JDK 1.2 version and is available in the java.util package. An ArrayList is used to store a dynamically sized collection of elements. An ArrayList grows in size automatically. Elements can be added, modified, and removed from an ArrayList whenever there is a need.
Java ArrayList Class Declaration
Here, E defines the type of elements that the List will contain.
Java ArrayList important features:
The Vector class was introduced in the Java 1.0 version and is available in the java.util package and implements the List interface. Vector is a synchronized and legacy class in Java.
A Vector is similar to an ArrayList in Java. A Vector is a dynamic array of objects which can grow or shrink in size as needed to accommodate adding and removing items after the Vector has been created.
The iterators returned by the Vector class are fail-fast. If the vector is structurally modified at any time after the iterator is created, in any way except through the iterator's own remove or add methods, the iterator will throw a ConcurrentModificationException.
Vector Class Declaration
Here, E defines the type of elements that the List will contain.
Important features of Vector:
Comparison Parameters | ArrayList Vs Vector |
Synchronization | ArrayList is not synchronized (not thread-safe), which means multiple threads can access an ArrayList object at the same time. Vector, on the other hand, is synchronised (thread-safe), so only one thread can access the vector object at a time. |
Performance | ArrayList is faster since it is non-synchronized, whereas Vector is by default synchronized so its operations give slower performance. |
Data Growth | ArrayList and Vector both grow and shrink dynamically to maintain optimal use of storage. ArrayList increases by 50% of the current array size if the number of elements exceeds its capacity, whereas Vector increases by 100%, essentially doubling the current array size. |
Traversal | ArrayList can only use iterator to traverse its elements, whereas Vector can use both enumeration and iterator to traverse its elements. |
Legacy Class | ArrayList was introduced in the JDK 1.2 version, and ArrayList is not a legacy class, whereas Vector is a legacy class. It was introduced in the JDK 1.0 version. |
Output
ArrayList Elements: [India, Canada, US, England]
Output
Vector Elements: [India, Canada, US, England]
That's all guys, hope this Java article is helpful for you.
Happy Learning... 😀
feedback@javabytechie.com