ArrayList and Vector both use Array as a data structure internally. However there are key differences between these classes. In this guide, you will learn the differences between ArrayList and Vector.
ArrayList Vs Vector: Differences between them
ArrayList | Vector |
---|---|
ArrayList is non-synchronized, which means multiple threads can work on ArrayList at the same time. For example: if one thread is performing an add operation on ArrayList, there can be an another thread performing remove operation on ArrayList at the same time in a multithreaded environment. | Vector is synchronized. This means if one thread is working on Vector, no other thread can get a hold of it. Unlike ArrayList, only one thread can perform an operation on vector at a time. |
ArrayList can grow and shrink dynamically, it grows by half of its size when resized. | Like ArrayList, Vector can grow and shrink dynamically, however it grows by double of its size when resized. |
ArrayList gives better performance (fast) for operations such as search, add, delete etc. This is because it is non-synchronized, which means multiple threads can perform different operations on it at the same time. | Vector is slow compared to ArrayList. Vector operations gives poor performance as they are thread-safe, the thread which works on Vector gets a lock on it which makes other thread wait till the lock is released. |
ArrayList is not a legacy class. | Vector is a legacy class. |
ArrayList uses iterator to traverse the elements. | Vector can use iterator as well as Enumeration to traverse the elements. |
Other key differences:
fail-fast: First let me explain what is fail-fast: If the collection (ArrayList, vector etc) gets structurally modified by any means, except the add or remove methods of iterator, after creation of iterator then the iterator will throw ConcurrentModificationException
. Structural modification refers to the addition or deletion of elements from the collection.
As per the Vector javadoc, the Enumeration returned by Vector is not fail-fast. On the other side the iterator and listIterator returned by ArrayList are fail-fast.
Legacy?: The vector was not the part of collection framework, it has been included in collections later. It can be considered as Legacy code. There is nothing about Vector which List collection cannot do. Therefore Vector should be avoided. If there is a need of thread-safe operation make ArrayList synchronized as discussed in the next section of this post or use CopyOnWriteArrayList which is a thread-safe variant of ArrayList.
There are few similarities between these classes which are as follows:
- Both Vector and ArrayList use growable array data structure.
- The iterator and listIterator returned by these classes (Vector and ArrayList) are fail-fast.
- They both are ordered collection classes as they maintain the elements insertion order.
- Vector & ArrayList both allows duplicate and null values.
- They both grows and shrinks automatically when overflow and deletion happens.
When to use ArrayList and when to use vector?
It totally depends on the requirement. If there is a need to perform “thread-safe” operation the vector is your best bet as it ensures that only one thread access the collection at a time.
Update: Even if you need to perform synchronized operations, you can still use ArrayList by converting it to a Synchronized ArrayList.
Performance: Synchronized operations consumes more time compared to non-synchronized ones so if there is no need for thread safe operation, ArrayList is a better choice as performance will be improved because of the concurrent processes.
How to make ArrayList synchronized?
As I stated above ArrayList methods are non-synchronized but still if there is a need you can make them synchronized like this:
//Use Collecions.synzhonizedList method List list = Collections.synchronizedList(new ArrayList()); ... //If you wanna use iterator on the synchronized list, use it //like this. It should be in synchronized block. synchronized (list) { Iterator iterator = list.iterator(); while (iterator.hasNext()) ... iterator.next(); ... }
Example of ArrayList in Java
In this example, we have an ArrayList that contains fruit names. We are iterating the arraylist using iterator.
import java.util.*; class JavaExample{ public static void main(String args[]){ List<String> fruits=new ArrayList<String>(); fruits.add("Apple"); fruits.add("Mango"); fruits.add("Orange"); fruits.add("Banana"); //Iterating the array list using iterator System.out.println("ArrayList elements: "); Iterator itr=fruits.iterator(); while(itr.hasNext()){ System.out.println(itr.next()); } } }
Output:
Example of Vector in Java
In this example, we have created a vector and added few elements to it. We are iterating this vector using enumeration.
import java.util.*; class JavaExample{ public static void main(String args[]){ Vector<String> names=new Vector<String>(); names.addElement("Chaitanya"); names.addElement("Ajeet"); names.addElement("Hari"); System.out.println("Vector elements: "); //Iterating vector using Enumeration Enumeration e=names.elements(); while(e.hasMoreElements()){ System.out.println(e.nextElement()); } } }
Output:
Recommended articles:
Akash says
1) Synchronization: ArrayList is non-synchronized
3) Performance: ArrayList gives better performance as it is synchronized.
Which one to believe?
Chaitanya Singh says
Hi Akash,
Thanks for bringing that to my attention. It was a typo. I fixed it :)
Regards,
Chaitanya
Akash says
can i get all these files in pdf? not so comfortable in reading online.
sandip soliya says
great job,thanks for making this very nice site…
Majid Arif Khakwani says
Thanks for info good comparison!!!!!!!!!
CB Singh says
The iterator and listIterator returned by these classes (Vector and ArrayList) are fail-fast.
I think above line is not correct in similarity section-
Because Vector is not fail fast and other thing Vector is using Enumerator not Iterator. Please correct if I am wrong.
santhu says
can u please explain about ConcurrentModificationException in detail…
Rishikesh Agrawani says
Your site is very useful and one of the best java’s knowledge provider site for beginners and advance programmers. The way you provide a brief and sufficient description about any topic is very good as examples are already included with almost each and everyone.I am a fan of your site.Its look is also good and the fonts used to write the topic’s description and program’s of java is too good.You have used the hyperlink among the description to provide special help about that topics, it’s good.I am reading java’s collection framework now and trying to take the benefit of your site.Very very thank you.I am very thankful to you…
Shakthi says
You have really made an excellent job thank you Chaitanya Singh ..Collections was really an nightmare but its no more.You really don’t know what a great help you have done to me.Thanks again!!!!!