Tuesday, September 25, 2012

ArrayList vs Vector in Java - Interview Question

ArrayList vs Vector in Java
ArrayList and Vector is two most widely used Collection class in Java and used to store object in ordered fashion. Every Java programmer which is introduced to Java Collection Framework either started with Vector or ArrayList. For beginners Difference between Vector and ArrayList in Java  and LinkedList vs ArrayList are two most  popular Java Interview question. ArrayList vs Vector is not only important on interview perspective but also on effective use of Java Collection API. After reading this article you will know when to use Vector in Java, When to use ArrayList in Java and would be able to compare ArrayList vs Vector over several important parameters e.g. Speed, Synchronization, Code quality etc. Before seeing difference on Vector vs ArrayList, let's What is common between the.

Common property of Vector and ArrayList in Java

1) Bother Vector and ArrayList are derived from AbstractList and implements List interface, which means both of them are ordered collection and allows duplicates.

2) Another similarity between Vector vs ArrayList is that both are index based Collection and you can use get(index) method to retrieve objects from Vector and ArrayList.

Vector vs ArrayList in Java

Vector vs ArrayList vs Vector in JavaIn last section we saw some common properties between both of them and its time to see How much ArrayList and Vector are different to each other.

1) First and most common difference between Vector vs ArrayList is that Vector is synchronized and thread-safe while ArrayList is neither Synchronized nor thread-safe. Now, What does that mean? It means if multiple thread try to access Vector same time they can do that without compromising Vector's internal state. Same is not true in case of ArrayList as methods like add(), remove() or get() is not synchronized.

2) Second major difference on Vector vs ArrayList is Speed, which is directly related to previous difference. Since Vector is synchronized, its slow and ArrayList is not synchronized its faster than Vector.

3) Third difference on Vector vs ArrayList is that Vector is a legacy class and initially it was not part of Java Collection Framework. From Java 1.4 Vector was retrofitted to implement List interface and become part of Collection Framework.

These were some comparison on Vector vs ArrayList. In Summary use ArrayList if you are using ArrayList in Single threaded environment and use Vector if you need a thread-safe collection. ArrayList is anytime faster than Vector in case thread-safety is not a concern.

Other Collection Interview Questions from Java67
Hashtable vs HashMap in Java
ArrayList vs HashMap in Java
wait vs sleep in Java
What is Inheritance in Java
10 tough core Java questions - Answered


  1. The correct answer should be... if you're still using Vectors, you need to crawl out from under your rock and take a look around. Vectors should not be used any more. See http://stackoverflow.com/questions/1386275/why-is-java-vector-class-considered-obsolete-or-deprecated for an explanation of why.

    1. Exactly, When to people start asking about questions like ArrayList vs Vector, HashMap vs Hashtable etc. Instead, they should ask about hashing, linked list data structure or at least when to use which kind of Collection is not too bad either.

  2. Vector is Synchronized, so how can multiple threads can access simultaneously ??? I think there is some mistake in the answer. Please correct it.


  3. vector is synchronized implies that, threads of same obj cant access simultaneously..but threads of different objects can do it.

  4. in all these years how many have opened ArrayList.java and Vector.java to check if there is more to those 3 differences. i did 10 years ago and founnd the 4th difference. may be i could find more if i look at them again !

  5. Instead of vector, its better to use synchronized ArrayList because Vector is now old and obsolete and can be removed in future verison.