ArrayList and Vector both are the classes under Collection Framework hierarchy. ArrayList and Vector, both are used to create a dynamic array of objects where the array can grow in size as and when required. There are two basic differences that distinguish ArrayList and Vector is that Vector belongs to Legacy classes that were later reengineered to support the collection classes whereas, an ArrayList is a standard collection class.
Another important difference is that ArrayList is non-synchronized on the other hand; Vector is synchronized. Let us study some other differences with the help of Comparison Chart shown below.
Content: Arraylist Vs Vector
|Basis for Comparison||ArrayList||Vector|
|Basic||ArrayList class is not Synchronized.||Vector class is synchronized.|
|Legacy class||ArrayList is a standard Collection class.||Vector is a legacy class, re-engineered to support the collection class.|
|Class Declaration||class ArrayList||class Vector|
|Reallocation||When not specified an ArrayList is incremented by half its size.||When not specified, a vector is incremented to double its size.|
|Performance||As ArrayList is unsynchronized, it operates faster than Vector.||As Vector is synchronized, it operates slower than ArrayList.|
|Enumeration/Iterator||ArrayList uses Iterator interface to traverse the objects stored in ArrayList.||Vector uses Enumeration as well as Iterator interface to traverse the objects stored in Vectors.|
Definition of ArrayList
ArrayList belongs to the list of standard collection classes. The class ArrayList is defined inside the java.util package, it extends the AbstractList class which is also a standard collection class, and it also implements List, an interface defined in Collection Interfaces. In Java, a standard array is always of fixed length. That means once created; it does not dynamically grow or shrink in size.
So, you should have the prior knowledge of the length of the array you are using. But, sometimes it may happen that the required length is revealed at runtime so, to handle this kind of situation java introduced ArrayList.
The ArrayList is class used for dynamic creation of an array that holds the references to the objects. This array could grow in size as and when required. The class declaration is as follow:
Here, E specifies the type of objects that an array will hold. The array created is of variable length, and it increases and decreases in size when objects are added or removed from the list.
The ArrayList is not synchronized that means, more than one thread can operate on the array at the same time. For example, if one thread is adding an object reference to the array and another thread is removing an object reference from the same array at the same time. The creation of a dynamic array using ArrayList class:
ArrayList<String> S1 = new ArrayList<String>( ); 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()); //OutputInitial 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
In the above code, you can see that; I created an array of objects of string type. I added some objects to the array S1 using add() method, and later deleted some objects using remove() method. You can observe if you do not specify the initial size of the array it will be of ‘0’ length. As you can see the array grows and shrink in size as you add and delete the elements.
Definition of Vector
Vector is a Legacy class which is reengineered to support the collection class in Collection Framework hierarchy. The vector class is also defined in java.util package, extended by AbstractList class and implemented by the List interface. The Vector class is declared as follow:
Here, the E defines the type of object that will be stored in an array. An array created using Vector class is of variable length. It increases double its size if the increment is not specified. Let’s understand the creation of array using Vector.
Vector<String> V = new Vector<String>(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
In above code you can see that, I particularly mentioned the size and increment value in the constructor of Vector respectively, while declaring the array of string objects. Hence, you can observe that as the limit of array finishes, it increments by the value provided to the constructor while declaration.
Key Differences Between ArrayList and Vectors
- Multiple threads could operate on ArrayList at the same time hence it is considered unsynchronized. Unlike ArrayList, only a single thread can operate on a vector at a time; hence it is called Synchronized.
- In an early version of Java, some classes and interfaces would provide the methods to store objects they were called Legacy classes Vector is one among the Legacy class of Java. Later, these legacy classes were reengineered to support Collection class whereas, ArrayList class is a standard Collection Class.
- When the limit of an array is fully utilized and a new object is added next to the exhausted array, its size grows in both the cases i.e. in ArrayList as well as in Vector but, the difference is that in ArrayList, if not specified the size is incremented by 50% of the current array whereas, in Vector array is doubled in size if the increment value is not specified.
- Vector uses Enumeration as well as Iterator to traverse an array whereas, an ArrayList only uses iterator for traversing an array.
- Since ArrayList is Unsynchronized and many threads can operate on it at the same time its performance is better than Vector on which only one thread can operate at a time.
- ArrayList and Vector both are defined in java.util package.
- ArrayList and Vector both extend AbsractList class.
- ArrayList and Vector both implements List interface.
- ArrayList and Vectors both are used to create a dynamic array that grows as required.
- ArrayList and Vector both hold object references.
I conclude by saying that use of ArrayList is better than using Vector as it performs faster and better.