Tuesday, September 6, 2011

Difference between Vector and ArrayList in Java

Arraylist and Vector are two of most used class on java collection package and difference between Vector and ArrayList is one of the most frequently asked interview question on first round or phone interview. Though it’s quite a simple question in my opinion but knowledge of when to use Vector over ArrayList or ArrayList over Vector does matter if you are working on a project. In this article we will some point based differences between Vector and ArrayList and trying to understand the concept behind those differences. Ultimate goal is to familiarize yourself with distinguish property of ArrayList and Vector.

Before seeing differences between Vector and ArrayList, let's see some similarities between these two and why we can use ArrayList in place of Vector on certain scenario.

arraylist and vector,difference between vector and arraylist1) Vector and ArrayList are index based and backed up by an array internally.
2) Both ArrayList and Vector maintains the insertion order of element. Means you can assume that you will get the object in the order you have inserted if you iterate over ArrayList or Vector.
3) Both iterator and ListIterator returned by ArrayList and Vector are fail-fast.

Now let's see some key differences between Vector and ArrayList in Java


Key Differences between Vector and ArrayList in Java

1) First and foremost difference is Vector is synchronized and ArrayList is not, what it means is that all the method which structurally modifies Vector e.g. add () or remove () are synchronized which makes it thread-safe and allows it to be used safely in a multi-threaded environment. On the other hand ArrayList methods are not synchronized thus not suitable for use in multi-threaded environment.

2) ArrayList is faster than Vector. Since Vector is synchronized it pays price of synchronization which makes it little slow. On the other hand ArrayList is not synchronized and fast which makes it obvious choice in a single-threaded access environment. You can also use ArrayList in a multi-threaded environment if multiple threads are only reading values from ArrayList.

3) Whenever Vector crossed the threshold specified it increases itself by value specified in capacityIncrement field while you can increase size of arrayList by calling ensureCapacity () method.

4) Vector can return enumeration of items it hold by calling elements () method which is not fail-fast as opposed to iterator and ListIterator returned by ArrayList.

5) Another point worth to remember is Vector is one of those classes which comes with JDK 1.0 and initially not part of Collection framework but in later version it's been re-factored to  implement List interface so that it could become part of collection framework



Conclusion is use ArrayList wherever possible and avoids use of Vector until you have no choice.

Related tutorials in Java
Do you like Books?
If you love to read books here are few Java titles which is worth of money :
Please vote +1 or consider sharing if your like this article

2 comments:

Anonymous said...

Hi Javin,
I have been visiting your blog for the past couple of weeks and has found it really nice. Though there are multiple such blogs the good thing about your blog seems to be that you have been regular in your posts and have followers who actively discuss things in the comments. Congrats on that :) Just an advice is that with in some posts the content seems to have got repeated. It could have been more concise and crisp without redundant info.

And I have a doubt regarding the point 5 mentioned in the above post:
>>
ArrayList in Java has no default size but Vector in Java has default size of 10.
<<
But I believe even ArrayList has a default size of 10. The javadoc says
>> ArrayList()
Constructs an empty list with an initial capacity of ten.
<<
Hope you will check on this and verify it.

All the best,
Lokesh.

Javin @ convert string to int in java said...

Thanks Lokesh for your valuable content, yes you are correct about point 5.Indeed javadoc of ArrayList says "Constructs an empty list with an initial capacity of ten." and I will correct that. Once again thanks for pointing this out and your kind advice. appreciate.

Post a Comment

What is this blog about

This blog is about my experience in Java, Tibco Rendezvous and FIX protocol. FIX is a technology which is used to build equity trading system and heavily used in electronic trading , high frequency trading and Algorithmic trading, so on most of Investment bank job you need to know FIX Protocol on the other hand Tibco Rendezvous is used to implement messaging backbone on many big commercial and global banks. This blog help you to get clear any FIX protocol job interview. It also includes Java and Tibco Rendezvous article written for new user mainly my experience in my own word.