Narayana Tutorial

Online Java Tutorial Blog

What is difference between Vector and ArrayList in Java?


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?

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.

Narayanaswamy

Hello! I am Narayanaswamy founder and admin of narayanatutorial.com. I have been working in IT industry more than 7 years. NarayanaTutorial is my web technologies blog. My specialties are Java / J2EE, Spring, Hibernate, Struts, Webservices, PHP, Oracle, MySQL, SQLServer, Web Hosting and Website Development.

I am a self learner and passionate about training and writing. I am always trying my best to share my knowledge through my blog.

Leave a Reply

Narayana Tutorial © 2018 Frontier Theme
Show Button
Hide Button