Thursday, May 25, 2023

Difference between ArrayList and Vector in Java

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

The ultimate goal is to familiarize yourself with the distinguishing property of ArrayList and VectorJava 5 also adds another implementation of the List interface which is similar to Vector and ArrayList but provides better concurrency access than Vector, it's is called CopyOnWriteArrayList

By the way, this is the third article discussing the Collection interview question,  Difference between LinkedList and ArrayList and   List vs Set are other popular interview questions based upon the collection framework in Java.

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

1) Vector and ArrayList are index-based and backed up by an array internally.
2) Both ArrayList and Vector maintain the insertion order of an element. This 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.
4) ArrayList and Vector also allows null and duplicates.




Vector vs ArrayList in Java

Now let's see some key differences between Vector and ArrayList in Java, this will decide when is the right time to use Vector over ArrayList and vice-versa. Differences are based upon properties like synchronization, thread-safety, speed, performance, navigation, and Iteration over List, etc.


1) Synchronization and thread-safety

First and foremost difference between Vector and ArrayList is that 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 and concurrent environment. 

On the other hand, ArrayList methods are not synchronized thus not suitable for use in a multi-threaded environment. This is also a popular interview question on a thread, where people ask why ArrayList can not be shared between multiple threads.

2) Speed and Performance

ArrayList is way faster than Vector. Since Vector is synchronized and thread-safe it pays the price of synchronization which makes it a little slow. On the other hand, ArrayList is not synchronized and fast which makes it an 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 or you can create read-only ArrayList as well.

3) Capacity

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

4) Enumeration and Iterator

Vector can return an enumeration of items it holds by calling elements () method which is not fail-fast as opposed to Iterator and ListIterator returned by ArrayList. I have discussed this point in detail on my post What is the difference between Iterator and Enumeration, you can also look there.

5) Legacy

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

And, here is a nice table to understand the difference between ArrayList and Vector class in Java:

Difference between ArrayList and Vector in Java



ArrayList vs Vector in JavaAfter considering these points about both Vector and ArrayList, my conclusion is to use ArrayList wherever possible and avoids the use of Vector until you have no choice. Think for CopyOnWriteArrayList over Vector, if you have multiple readers and few writers because it can provide thread safety without impacting performance too much.


Other Java tutorials from Javarevisited

7 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.

Podolski said...

If you compare Vector vs ArrayList or Vector vs HashSet you will find ArrayList and HashSet are way faster than Vector and given we have Concurrent List implementation like CopyOnWriteArrayList, there is no need to use Vector.Only significant difference between Vector and ArrayList is synchronization or locking otherwise they are same.

Anonymous said...

The content of your blog with easy to understand language makes your blog a really useful place for all Java Developers.

Sekar said...

Good article. Keep it up...

Anonymous said...

Conflicting points in similarity and difference that vector is fail-fast.
It is fail-safe.

java67 said...

Instead of using vector, you can also use synchronized ArrayList, its better option because vector is old class and any new optimization will more likely to go ArrayList than Vector.

Post a Comment