Difference between ArrayList and Vector In java

  1. Home
  2. vector maps manual
  3. Difference between ArrayList and Vector In java

ArrayList and Vector both use Array as a data structure internally. However there are few differences in the way they store and process the data. In this post we will discuss the difference and similarities between ArrayList and Vector. 1) Synchronization: ArrayList is non-synchronized which means multiple threads can work on ArrayList at the same time. For e.g. if one thread is performing an add operation on ArrayList, there can be an another thread performing remove operation on ArrayList at the same time in a multithreaded environment while Vector is synchronized. This means if one thread is working on Vector, no other thread can get a hold of it. Unlike ArrayList, only one thread can perform an operation on vector at a time. 2) Resize: Both ArrayList and Vector can grow and shrink dynamically to maintain the optimal use of storage, however the way they resized is different. ArrayList grow by half of its size when resized while Vector doubles the size of itself by default when grows. 3) Performance: ArrayList gives better performance as it is non-synchronized. Vector operations gives poor performance as they are thread-safe, the thread which works on Vector gets a lock on it which makes other thread wait till the lock is released. 4) fail-fast: First let me explain what is fail-fast: If the collection (ArrayList, vector etc) gets structurally modified by any means, except the add or remove methods of iterator, after creation of iterator then the iterator will throw ConcurrentModificationException. Structural modification refers to the addition or deletion of elements from the collection. As per the Vector javadoc the Enumeration returned by Vector is not fail-fast. On the other side the iterator and listIterator returned by ArrayList are fail-fast. 5) Who belongs to collection framework really? The vector was not the part of collection framework, it has been included in collections later. It can be considered as Legacy code. There is nothing about Vector which List collection cannot do. Therefore Vector should be avoided. If there is a need of thread-safe operation make ArrayList synchronized as discussed in the next section of this post or use CopyOnWriteArrayList which is a thread-safe variant of ArrayList. It totally depends on the requirement. If there is a need to perform “thread-safe” operation the vector is your best bet as it ensures that only one thread access the collection at a time. Performance: Synchronized operations consumes more time compared to non-synchronized ones so if there is no need for thread safe operation, ArrayList is a better choice as performance will be improved because of the concurrent processes. How to make ArrayList synchronized? As I stated above ArrayList methods are non-synchronized but still if there is a need you can make them synchronized like this – The iterator and listIterator returned by these classes (Vector and ArrayList) are fail-fast. I think above line is not correct in similarity section- Because Vector is not fail fast and other thing Vector is using Enumerator not Iterator. Please correct if I am wrong. Your site is very useful and one of the best java’s knowledge provider site for beginners and advance programmers. The way you provide a brief and sufficient description about any topic is very good as examples are already included with almost each and everyone.I am a fan of your site.Its look is also good and the fonts used to write the topic’s description and program’s of java is too good.You have used the hyperlink among the description to provide special help about that topics, it’s good.I am reading java’s collection framework now and trying to take the benefit of your site.Very very thank you.I am very thankful to you… You have really made an excellent job thank you Chaitanya Singh ..Collections was really an nightmare but its no more.You really don’t know what a great help you have done to me.Thanks again!!!!! Founder of beginnersBook.com, loves Java and open source stuff. BeginnersBook.com is a tech blog where he shares tutorials on programming (Java, C, CPP), WordPress, SEO and web development. Source.

whatsapp button