1. Synchronization
ArrayList is not Synchronized, That means multiple Threads can work on ArrayList Object at a same time. For e.g. if one thread is performing an add operation on ArrayList, there can be an another thread performing remove operation on ArrayList at the same time in a multithreaded environment.
Vector is Synchronized, That means if one Thread is working on it then other Thread can not work on until previous Thread release it.
2. Resize
Both are dynamic in size, But when they reached to its max limit and resize of storage is different. ArrayList grows by half [50%] of its size when resized while Vector doubles[100%] the size of itself when resized.
3. Performance
ArrayList gives better performance compare to Vector as its not Synchronized. Vector having poor performance as it is Synchronized, Thread which works on Vector holds lock on it which makes other Thread to wait until it release.
4. Enumerator
Other than Hashtable ,Vector is the only other class which uses both Enumeration and Iterator .While ArrayList can only use Iterator for traversing an ArrayList.
5. Introduction in Java
ArrayList was introduced in Java Version 1.2 however Vector was present since first version of Java.
Vector is a legacy class. Its re-structured and re-engineered to support generic from Java 1.5 version onward.
6. Example
0 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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
package com.codenuclear; import java.util.ArrayList; import java.util.Enumeration; import java.util.Iterator; import java.util.List; import java.util.Vector; public class ArrayListVectorExample { public static void main(String args[]){ /** * Creation of ArrayList */ List<String> arrayListObj=new ArrayList<String>(); arrayListObj.add("John");//adding object in arraylist arrayListObj.add("Rob"); /** * Iterating ArrayList through Iterator */ Iterator itr=arrayListObj.iterator(); while(itr.hasNext()){ System.out.println(itr.next()); } /** * Creation of Vector */ Vector<String> vectorObj=new Vector<String>(); vectorObj.add("Michel");//method of Collection vectorObj.addElement("Walter"); /** * Iterating Vector through Enumeration */ Enumeration e=vectorObj.elements(); while(e.hasMoreElements()){ System.out.println(e.nextElement()); } } } |