Newsletter |
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() Collections.synchronizedList(li)
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).
You Might Also Like
::. About the Author .:: | ||
If we synchronize arraylist,which is very poor in performance right?please explain me why?
If We Can Synchornize ArrayList , then what is the use of Vector? which Senario we use the vector then?
what is the Advantage of default initial capacity in ArrayList?
What are the initial sizes for both ArrayList & Vector ?
what is the default capacity in vector ?
How to generate the Barcode in jsp. please send me sample application using to eclipse?
Hi ,
please code for spring mvc with hibernate for CRUD operations
Can we make arraylist as synchronized?If yes,what is the use of Vector ?
when should i go for vector?
Hello sir
this is really very helpful and nice article about arraylist and vector in java.
thank you so much for this post
please keep posting for us.