Monday, July 20, 2015

ArrayList vs Vector in java

Most of the developers are still not sure if they have to use a Vector or an ArrayList. As from my experience we cannot state a general statement that option A is better than option B. But here i outline some important things to check before choosing either of them.

1 . Vector is synchronized , While ArrayList is not. Which means that all operations done on a Vector is thread safe by default and you don't have to provide any explicit synchronization . So i would prefer Vector over ArrayList when i am working with threads. But this has a negetive side . Vector has synchronization code which makes it bit less performing in normal cases where thread safety is not a concern
 
2 . The size of a ArrayList increases 50% , While Vector it doubles as the data goes out of bounds . You have to understand the pattern in which the data grows to consider which is better. Remember Vector has an option to set the increment value .

3. Retrieving elements from a particular position is of the O(1) , in both ArrayList and Vector . However both suffers a penalty of O(n) to insert an element to the middle mainly because one have to shift all n-i element to insert an element at ith Position.



Hope you will look into these points from now on .  Happy coding :)

No comments:

Post a Comment