Here is list of my favorite, frequently asked Questions from Java collection framework. Almost all of these questions have appeared in Java interview at various level ranging from Junior to Senior software engineer level.
ArrayList and Vector are two of most used class on java collection package
What is difference between Vector and ArrayList in Java?
Table of Contents
- Vector and ArrayList are index based and backed up by an array internally.
- Both ArrayList and Vector maintains the insertion order of element. Means you can assume that you will get the object in the order you have inserted if you iterate over ArrayList or Vector .
- Both Iterator and ListIterator returned by ArrayList and Vector are fail-fast.
- ArrayList and Vector also allows null and duplicates.
Synchronization and thread-safety
- Vector is synchronized by default and thread-safety
- ArrayList is not synchronized and not thread-safety
- Vector is better choice in the multi-threaded case.
- ArrayList is better choice in the single-threaded case.
only one thread can call methods on a Vector at a time and there is s a slight overhead in acquiring the lock.
If you use an ArrayList , this is not the case.
Also note that if you have an ArrayList , you can use the Collections.synchronizedList function to create a synchronized list, thus getting you the equivalent of a Vector.