In this blog we gonna talk about the difference between ArrayList and Vector. Both ArrayList and Vector are classes in the hierarchy of the Collection Framework. Both ArrayList and Vector are used to build dynamic arrays of items that can expand in size as needed. The two main distinctions between an ArrayList and a Vector are that an ArrayList is a standard collection class while a Vector belongs to a legacy class that was subsequently redesigned to enable collection classes. 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:
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:
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());
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:
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.
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());
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
|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.|
- 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.
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.