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
Similarities
- 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.
Differences
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.
Hello! I am Narayanaswamy founder and admin of narayanatutorial.com. I have been working in the IT industry for more than 12 years. NarayanaTutorial is my web technologies blog. My specialties are Java / J2EE, Spring, Hibernate, Struts, Webservices, PHP, Oracle, MySQL, SQLServer, Web Hosting, Website Development, and IAM(ForgeRock) Specialist
I am a self-learner and passionate about training and writing. I am always trying my best to share my knowledge through my blog.