Differences between ArrayList and Vector
ArrayList and Vector difference is very common Java interview question. While beginners tend to use ArrayList for any kind of requirement, the attributes of Vector class should also not be ignored. By having good grasp of both the classes, you can write code which is apt to the requirements.
Similar to ArrayList vs Vector, HashMap vs Hastable is also very frequently asked in interviews. It is good to know the comparison between closely looking classes.
- While ArrayList doesn’t provide any thread-safety, Vector class has its methods synchronized. This means that calls to member methods on a vector instance are not interleaved where as the calls to member methods of ArrayList class are interleaved. Any method calls on ArrayList need to be synchronized externally by the use synchronized keyword or JDK 5 synchronized collection classes or Apache Commons Collection library.
- Due to added thread safety by the use of synchronized member methods, the Vector class degrades performance. This performance lag can be seen in multi-threaded applications. When two different threads invoke the same method on the same Vector instance, the calls cannot be interleaved. One thread needs to wait until the other completes its execution of Vector member method.
- The ArrayList class was introduced in JDK 1.2 where as Vector class has been part of Java since beginning (JDK 1.0).
- With ArrayList we can only specify the initial capacity. But with Vector, we can not only specify the initial capacity but also capacity increment value.
- Both the classes allocate more memory to the internal array member variable when initial capacity is overrun. But there are differences how more memory is allocated in two classes. The ArrayList class increases its size by half the initial capacity where as Vector increases its size by the capacity increment value. If the value of capacity increment is 0, the vector increases its size by double the initial capacity in the vector.
- Developer doesn’t need to handle space time trade off in case of ArrayList. Since Vector allows setting the capacity increment value, setting a very small increment value can result in wastage of processing done in allocating more memory. It is always advisable to set the initial capacity as the optimum elements that the collection class will ever hold.
- The use of Vector class is not encouraged only for synchronized feature. You should use Vector only when dealing with some legacy applications. ArrayList can be used without any restriction in single threaded classes. If you know the size of elements to be stored and don’t need the collection to grow dynamically, it is better to use array instead of ArrayList.
- Authors of Vector class are Lee Boynton and Jonathan Payne, while ArrayList has been written by Josh Bloch and Neal Gafter.
- The serialversionUID for Vector class has been carried over from JDK1.0 as -2767605614048989439L while ArrayList has a serialversionUID of 8683452581122892189L.
- Both ArrayList and Vector support iterator as both of them extend from AbstractList class and implement List interface. But Vector has support for getting an enumeration over its elements. The iterator returned by both the classes are fail fast where as the enumeration returned by the Vector is not fail-fast.