vector vs arraylist in java

Java’s collection framework is incredibly useful. Some commonly used collections are ArrayList, Vector, LinkedList, HashMap, and so on.

ArrayList is a dynamically resizing array, which grows as you insert elements. Vector is very similar to an ArrayList, except that it is synchronized. Its syntax is almost identical as well.

ArrayList and Vectors both implement the List interface and both use (dynamically resizable) arrays for its internal data structure, much like using an ordinary array.

Major Differences between ArrayList and Vector:

  1. Synchronization: Vector is synchronized, which means only one thread at a time can access the code, while arrayList is not synchronized, which means multiple threads can work on arrayList at the same time. For example, if one thread is performing an add operation, then there can be another thread performing a remove operation in a multithreading environment.
    If multiple threads access arrayList concurrently, then we must synchronize the block of the code which modifies the list structurally, or alternatively allow simple element modifications. Structural modification means addition or deletion of element(s) from the list. Setting the value of an existing element is not a structural modification.
  2. Performance: ArrayList is faster, since it is non-synchronized, while vector operations give slower performance since they are synchronized (thread-safe). If one thread works on a vector, it has acquired a lock on it, which forces any other thread wanting to work on it to have to wait until the lock is released.
  3. Data Growth: ArrayList and Vector both grow and shrink dynamically to maintain optimal use of storage – but the way they resize is different. ArrayList increments 50% of the current array size if the number of elements exceeds its capacity, while vector increments 100% – essentially doubling the current array size.
  4. Traversal: Vector can use both Enumeration and Iterator for traversing over elements of vector while ArrayList can only use Iterator for traversing.

In a word, ArrayList is faster(non-synchronized), while Vector is safer(synchronized).

In practical, ArrayList is preferable when there is no specific requirement to use vector.

Here is a Java program to illustrate the use of ArrayList and Vector in Java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import java.util.*;

public class {

public static void main(String[] args) {

ArrayList<String> myArr = new ArrayList<>();
myArr.add("one");
myArr.add("two");
myArr.add("three");
myArr.add("four");
myArr.add("five");
// traversing using iterator
Iterator it = myArr.iterator();
System.out.println("### Traversing ArrayList using Iterator:");
while (it.hasNext()){
System.out.println(it.next());
}

// new a Vector
Vector<String> myVec = new Vector<>();
myVec.add("piano");
myVec.add("guitar");
myVec.add("violin");
// traversing using enumerator
Enumeration enu = myVec.elements();
System.out.println("### Traversing Vector using Enumeration:");
while (enu.hasMoreElements()){
System.out.println(enu.nextElement());
}
}
}

Output:

1
2
3
4
5
6
7
8
9
10
### Traversing ArrayList using Iterator:
one
two
three
four
five
### Traversing Vector using Enumeration:
piano
guitar
violin

References

GeeksforGeeks - Vector vs ArrayList in Java
Cracking the coding interview 6th
GeeksforGeeks - Collections in Java