Last Updated on September 22, 2023 by Mayank Dham
In this blog we gonna talk about the difference between ArrayList and Vector. ArrayList and Vector are both members of the Collection Framework’s class hierarchy. They serve as tools to construct dynamic arrays that can dynamically adjust their size. However, two primary disparities between ArrayList and Vector exist. Specifically, ArrayList stands as a conventional collection class, whereas Vector has origins as a legacy class that was subsequently adapted to accommodate collection class functionality. Let us deep dive into our topic i.e the difference between ArrayList and Vector.
Another significant distinction is that whereas Vector is synchronized, ArrayList is non-synchronized.
Definition of ArrayList
One of the common collection classes is ArrayList. The ArrayList class is a standard collection class that extends AbstractList and implements the List interface from Collection Interfaces. It is specified inside java.util package. A typical array in Java is always a fixed length. This means that once something is formed, its size does not change on demand.
Therefore, you should be aware of the size of the array you are utilizing. However, it is possible that the requisite length will occasionally become clear during use, hence Java added ArrayList to address this type of circumstance.
A class called ArrayList is used to dynamically create an array that contains references to the items. The size of this array might expand as needed. The declaration of the class is as follows:
class ArrayList
In this case, E designates the kind of objects that an array will include. When objects are added to or removed from the list, a variable-length array is produced and its size changes.
More than one thread may use the array at once since the ArrayList has not synchronized. For instance, suppose two threads are simultaneously adding object references to the array and deleting object references from the same array. Using the ArrayList class, a dynamic array may be created:
ArrayList S1 = new ArrayList( );
System.out.println("Initial size of S1: " +S1.size());
S1.add("T");
S1.add("C");
S1.add("H");
S1.add(1,"E");
System.out.println("After addition S1 contains : " + S1);
System.out.println("Size of S1 after addition: " +S1.size());
S1.remove( "T");
S1.remove (2);
System.out.println("After deletion S1 contains : " + S1);
System.out.println("Size of S1 after deletion: " +S1.size());
Output
Initial size of S1: 0
After addition S1 contains: [T, E, C, H];
Size of S1 after addition: 4
After deletion S1 contains: [E, H]
Size of S1 after deletion: 2
You can see from the code above that I made an array of string-type objects. I used the add() function to add certain items to the array S1 and the remove() method to remove some of those same things. You can see that the array will have a length of "0" if the starting size is left blank. As you can see, as elements are added and removed, the array’s size increases and decreases.
Definition of Vector
Vector is a legacy class that has been redesigned to support the collection class in the hierarchy of the Collection Framework. The AbstractList class extends the vector class in java.util package and the List interface implements the interface. This is how the Vector class is declared:
class Vector
The E in this case specifies the kind of item that will be kept in the array. The length of an array made with the Vector class is flexible. If the increment is not supplied, it doubles in size. Let’s see how a vector may be used to create an array.
Vector V = new Vector(1,1);
V.addElement("Tech");
V.addElement("Differences");
System.out.println("Capacity after 2 addition: " +V.capacity());
V.addElement("Between");
V.addElement("Vectors");
System.out.println("Current capacity: " +V.capacity());
Output
Capacity after 2 addition: 2
Current capacity: 4
You can see from the code that I specifically indicated the size and increment value in the constructors of the Vector and string object arrays, respectively. You can see that the array increments by the value passed to the function Object() at declaration as the array’s maximum are reached.
Difference between ArrayList and Vector
ArrayList | Vector |
---|---|
1) The ArrayList is not synced. | The vector has been synced. |
2) If the array’s capacity is exceeded, ArrayList increases the size of the array by 50%. | If the total number of items exceeds the array’s limit, a vector increment of 100% signifies that the array size is doubled. |
3) It is not a legacy class, ArrayList. It first appears in JDK 1.2. | The class Vector is an old one. |
4) Since ArrayList is not synchronized, it is quick. | Due to its synchronization, the vector is slower since it keeps other threads in a runnable or non-runnable state in a multithreading environment until the current thread releases the lock on the object. |
5) The Iterator interface is used by ArrayList to iterate across the entries. | A Vector can navigate the components using either the Iterator interface or the Enumeration interface. |
Similarities
- Vector and ArrayList are both defined in java.util package.
- Both the classes ArrayList and Vector extend AbsractList.
- The List interface is implemented by both ArrayList and Vector.
- A dynamic array that expands as needed is made using both ArrayList and Vectors.
- Both ArrayList and Vector contain references to objects.
Conclusion
So with this we end this article as we discussed the difference between ArrayList and Vector Also, I conclude by saying that the use of ArrayList is better than using Vector as it performs faster and better.
FAQs related to Difference Between ArrayList and Vector
Here are some of the Difference Between ArrayList and Vector
1. What is the main difference between ArrayList and Vector?
The primary difference lies in their synchronization behavior. ArrayList is unsynchronized, while Vector is synchronized.
2. When should I use ArrayList over Vector?
Use ArrayList when you don’t need built-in synchronization and want better performance in single-threaded environments.
3. When should I use Vector over ArrayList?
If you require thread safety and need a dynamic array structure, Vector might be preferable, but be aware of the potential performance impact.
4. Can I use ArrayList and Vector interchangeably?
Yes, you can use them interchangeably in most cases, but you should be mindful of synchronization when dealing with Vector.
5. Is Vector recommended for modern Java development?
Not necessarily. In modern Java, where explicit synchronization is discouraged in favor of more fine-grained concurrency control, Vector might not be the first choice due to its default synchronization.