Difference between Vector and ArrayList
Key Difference: Vector and ArrayList are both classes from the Java collection package. Vector is used to implement a dynamic array which grows automatically as per need basis. Like Vector, ArrayList is also an implementation of list interface. Vector is synchronized, whereas ArrayList is not synchronized.
include("ad4th.php"); ?>Vector is the name of a class that is present in java.util package of Java. It implements a dynamic array that grows on its own according to the requirement. It does not need any fix dimensions. There are various methods contained in the vector class. Add() method is used to add elements in vector. For this purpose add(index,object) method is used. This adds the mentioned object at the mentioned index. Vector is synchronized which means that at a particular time, only one thread is able to access its method from outside. Thus, vector is considered to be thread-safe.
Example -
public class ExampleVector {
public static void main(String[] args)
{
Vector a = new Vector();
a.add("20");
a.add("30");
System.out.println(a.get(0));
System.out.println(a.get(1));
}
}
Output –
20
30
include("ad3rd.php"); ?>Like Vector, ArrayList is also an implementation of list interface. An ordered group of elements can be stored in a java array list and duplicates are also allowed. ArrayList( ), ArrayList (Collection c) or ArrayList(int size) can be used to create an instance of ArrayList. Vector and ArrayList are same in many terms like both are index based and are implementation of an array internally. The order of insertion remains intact in both the cases. Both allow null and duplicates. Still, both are different in some contexts. The major point of difference is regarding the synchronization. Vectors are synchronized and ArrayLists are not synchronized. They also differ in terms of data growth. By default, a vector doubles the size of its array. On the other hand, ArrayList increases its size by 50 percent. Let us find some point of differences in the table below -
Comparison between Vector and Arraylist:
|
Vector |
Arraylist |
Definition |
Vector is the name of a class that is present in java.util package of Java. It implements a dynamic array that grows on its own according to the requirement. |
ArrayList is also an implementation of list interface. An ordered group of elements can be stored in a java array list and duplicates are also allowed. |
Synchronisation |
Yes |
No |
Speed |
Slower |
Faster |
Capacity |
On crossing the specified threshold, it increases itself according to the value mention in the field known as capacityIncrement |
ensureCapacity() method is called in order to increase the size of ArrayList |
Returned Iterators |
Fail-fast |
Fail-fast |
Enumerator |
Not fail-fast |
Fail-fast |
Images Courtesy: aspdotnet-sekhar.blogspot.com
Add new comment