Friday, 22 April 2016

Vector vs. ArrayList : Which is better and Why ?


SYNCHRONIZATION
Vectors are synchronized, but incurs a performance hit. ArrayList is not synchronized.
If you don't need a thread-safe collection, use the ArrayList.

DEFAULT SIZE
ArrayList has no default size while vector has a default size of 10.

DATA GROWTH
When you insert an element into an ArrayList or a Vector, it expends its size, if it runs out of room.
By default, Vector doubles the size of its array, while ArrayList increases its size by 50% 
In Vector, you can set increment size.

TRAVERSAL
For traversing an ArrayList, you can simply use an index instead of having to create an iterator.  
Vector requires an iterator.

USAGE PATTERN
Retrieving elements can be done in constant time O(1) and adding / removing elements from any other position is more expensive O(n-i) using ArrayList and Vector both.
If you want to index elements or add and remove elements at the end of the array, use Vector or ArrayList.
In order to add / delete element at any position, prefer to use LinkedList over both.

No comments:

Post a Comment

Note: only a member of this blog may post a comment.