Difference between Arraylist and Vector in Java
Hi friends, let us see the main differences between ArrayList and Vector collections, this is very important and common question in interviews for all IT people, hope you might face this question in your previous interviews
Any how let us see the main differences and which is better regarding the performance
- ArrayList and Vector both having same data structure internally, which is Array
- Vector is by default synchronized, means at a time only one thread can access its methods from out side, where as ArrayList is non-synchronized means N number of threads can access at a time
- But we can make ArrayList as synchronized by using Collections class, see it bellow
- Both Vector and ArrayList have capability to re-size dynamically, means Vector will Doubles the size of its array when its size increased, but ArrayList increased by Half only
How to make ArrayList Synchronized
As we discussed above ArrayList is not synchronized by default, so will see how to make it as Synchronized.
We have direct method in Collections class to make ArrayList synchronized..
List li = new ArrayList()
ArrayList vs Vector Speed and Performance Differences
Always ArrayList will shows better performance compared to Vector, except Synchronization both are almost same in their performance. Vector is Synchronized means thread safe, only 1 thread can access so its very slow compared to ArrayList, because in our real time projects we should not require synchronized methods always.
What am saying is always try to use ArrayList rather Vector if its not required any Synchronization in your requirements, even if so you know how to make ArrayList as synchronized right (just like above).