Difference between ArrayList and Vector in Java

Java ArrayList and Vector both implements List interface and maintains insertion order. But there are some differences between ArrayList and Vector.
  1. Thread Safety (Synchronization)
  2. Data growth
  3. Performance
  4. Traversing (Iterator)
  5. Legacy

Thread Safety (synchronized)

Vector is synchronized by default, and ArrayList is not.

What is Synchronization ?

When we start two or more threads within a program, there may be a situation when multiple threads try to access the same resource and finally they can produce unforeseen result due to concurrency issues. The synchronized keyword causes a thread to obtain a lock when entering the method, so that only one thread can execute the method at the same time. This is usually called making the class thread-safe . Without synchronization, it is not guaranteed in which order the reads and writes happen, possibly leaving the variable with garbage . What this (Synchronization) means is that only one thread can call methods on a Vector at a time, and there's a slight overhead in acquiring the lock. It is important to note that, you can make ArrayList also synchronized by passing arraylist object to Collections.synchronizedList() method.

Data growth

Internally, both the ArrayList and Vector hold onto their contents using an Array . Both can grow and shrink dynamically to maintain the optimal use of storage, however the way they resized is different. ArrayList is created with an default initial size of 10. When this size is exceeded, the collection is automatically increases to half of the default size that is 15. Vector increments 100% means doubles the array size if total number of element exceeds than its capacity.

Performance

ArrayList gives better performance as it is non-synchronized . Vector operations gives poor performance as they are thread-safe , the thread which works on Vector gets a lock on it which makes other thread wait till the lock is released. That means, in ArrayList two or more threads can access the code at the same time , while Vector is limited to one thread at a time.

Traversing (Iterator)

ArrayList elements can be traversed using Iterator , ListIterator and using either normal or advanced for loop. Vector uses Enumeration interface to traverse the elements.

Legacy

The vector was not the part of collection framework , it has been included in collections later. It can be considered as Legacy code. There is nothing about Vector which List collection cannot do. Therefore Vector should be avoided.

ArrayList Implementation

import java.util.*; class TestClass { public static void main (String[] args) { // create an array list Object ArrayList aList = new ArrayList(); aList.add("Sunday"); //adding item aList.add("Monday"); aList.add("Tuesday"); Iterator ir=aList.iterator(); while(ir.hasNext()){ System.out.println(ir.next()); } }
Output
Sunday Monday Tuesday

Vector Implementation

import java.util.*; class TestClass { public static void main (String[] args) { //create new Vector Object Vector vcTr = new Vector(); System.out.println("Vector Size: " + vcTr.size()); // initial size is 2, increment is 2 vcTr = new Vector(2,2); System.out.println("Vector Size: " + vcTr.size()); vcTr.addElement("Sunday"); vcTr.addElement("Monday"); vcTr.addElement("Wednesday"); System.out.println("Vector Size: " + vcTr.size()); System.out.println("Vector Capacity: " + vcTr.capacity()); //you can see here the capacity doubled as 4 //using Iterator to see all elemnets in vector Iterator < String > itr = vcTr.iterator(); while(itr.hasNext()){ System.out.println(itr.next()); } } }
Output
Vector Size: 0 Vector Size: 0 Vector Size: 3 Vector Capacity: 4 Sunday Monday Wednesday