Computer >> Computer tutorials >  >> Programming >> Java

Differences between ArrayList and Vector in Java


Both ArrayList and Vector are implementation of List interface in Java. Both classes keeps the insertion order. But there are certain differences as well.

Following are the important differences between ArrayList and Vector method.

Sr. No.
Key
ArrayList
Vector
1
Synchronization
ArrayList is non-synchronized.
Vector is synchronized.
2
Size
ArrayList increments 50% of its current size if element added exceeds its capacity.
Vector increments 100% of its current size if element added exceeds its capacity.
3
Legacy
ArrayList is not legacy.
Vector is a legacy class.
4
Speed
ArrayList is faster being non-syncronized.
LinkedList is slower being syncronized.
5
Iteration
ArrayList uses iterator interface to traverse through elements.
Vector can use both iterator or enumerator interface to traverse through elements.

Example of ArrayList vs Vector

JavaTester.java

import java.util.ArrayList;
import java.util.Vector;
import java.util.List;
public class JavaTester {
   public static void main(String args[]) {
      List<String> list = new ArrayList<>();
      list.add("A");
      list.add("B");
      list.add("C");
      list.add("D");
      List<String> list1 = new Vector<>();
      list1.add("A");
      list1.add("B");
      list1.add("C");
      list1.add("D");
      System.out.println(list);
      System.out.println(list1);
   }
}

Output

[A, B, C, D]
[A, B, C, D]