Differences and Similarities between ArrayList and vector in java


It’s very important to differentiate between ArrayList and Vector, so in this Collection framework tutorial we will learn what are differences and similarities between java.util.ArrayList and java.util.Vector in java.




Differences between java.util.ArrayList and java.util.Vector in java>


Property
java.util.ArrayList
java.util.Vector
1
synchronization
java.util.ArrayList is not synchronized  (because 2 threads on same ArrayList object can access it at same time).

I have created program to show consequence of using ArrayList in multithreading environment.
In the program we will implement our own arrayList in java.
java.util.Vector is synchronized (because 2 threads on same Vector object cannot  access it at same time).

I have created program to show advantage of using Vector in multithreading environment.
In the program we will implement our own vector in java.
2
Performance
ArrayList is not synchronized, hence its operations are faster as compared to Vector in java.
Vector is synchronized, hence its operations are slower as compared to ArrayList in java.

If we are working not working in multithreading environment jdk recommends us to use ArrayList.
3
Enumeration
Enumeration is fail-fast, means any modification made to ArrayList during iteration using Enumeration will throw ConcurrentModificationException in java.

Enumeration is fail-safe, means any modification made to Vector during iteration using Enumeration don’t throw any exception in java.

4
Introduced  in which java version
ArrayList was introduced in second version of java i.e. JDK 2.0
Vector was introduced in first version of java i.e. JDK 1.0
But it was refactored in java 2 i.e. JDK 1.2 to implement the List interface, hence making it a member of member of the Java Collections Framework.
5
Ensuring Capacity/ resizing.
ArrayList is created with initial capacity of 10.
When its full size is increased by 50% i.e. after resizing it’s size become 15 in java.
Vector is created with initial capacity of 10.
Vector’s size is increased by 100% i.e. after resizing it’s size become 20 in java.
6
Custom implementation
Read :



So far we have learned what are differences between ArrayList and Vector in java. Now we will learn similarities in ArrayList and Vector in Collection framework in java.



/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */


Similarity in java.util.ArrayList and java.util.Vector in java >

Property
java.util.ArrayList and java.util.Vector
1
Iterator and listIterator are Fail-fast
Iterator and listIterator returned by ArrayList and Vector both are Fail-fast in java.
2
Insertion order
ArrayList and Vector both maintains insertion order in java.
3
Duplicate
ArrayList and Vector both allow to store duplicate elements in java.
4
Allows null
ArrayList and Vector both allows to store null in java.
5
Implements java.util.List
ArrayList and Vector both are implementation of the java.util.List interface.
6
Index based structure
ArrayList and Vector both are index based structures in java.
7
Complexity
Complexity offered by methods of ArrayList and Vector both of these is same in java.

Operation/ method
Worst case
Best case
add
O(n), when array is full it needs restructuring,
operation runs in amortized constant time.
O(1), when array does not need any restructuring.
remove
O(n), when removal is done from between restructuring is needed.
O(1), when removal is done at last position, no restructuring is needed.
get
O(1), it is index based structure. So, complexity of  get operation is always done in O(1).
O(1) it is index based structure. So, complexity of  get operation is always done in O(1).
set
O(1), it is index based structure, no restructuring is needed in set operation. So, complexity of operation is always O(1)
O(1), it is index based structure, no restructuring is needed in set operation. So, complexity of operation is always O(1)
iterator
O(n), because ireation is done over each and every element.
O(n), because ireation is done over each and every element.
listIterator
O(n), its same as iterator.
O(n), its same as iterator.
enumeration
O(n), its same as iterator.
O(n), its same as iterator.


So in this Collection framework tutorial we learned what are important differences and similarities between java.util.ArrayList and java.util.Vector in java.


Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.



RELATED LINKS>

ConcurrentModificationException, Fail-fast and Fail-safe in detail in java



Important Similarity and Differences >

List Differences >

List vs Set - Similarity and Differences in java

HashMap and Hashtable - Similarity and Differences in java


Iterator vs ListIterator - Similarity and Differences in java


Iterator vs Enumeration - Differences and similarities in java




Important Similarity and Differences Collection classes in concurrent and non-concurrent packages >

ArrayList vs CopyOnWriteArrayList - Similarity and Differences with program in java



List hierarchy tutorial in java - Detailed - java.util.ArrayList, java.util.LinkedList, java.util.vector, java.util.concurrent.CopyOnWriteArrayList classes


eEdit
Must read for you :