Kamis, 20 November 2014

Difference between ArrayList and Vector in java

Difference between Vector and ArrayList is the most common Core Java Interview question you will come across in Collection. This question is mostly used as a start up question by the Interviewers before testing deep roots of the Collection.

Before seeing differences between Vector and ArrayList, let's see some similarities between these two and why we can use ArrayList in place of Vector on certain scenario.

Similarities between Vector and ArrayList


  1. Vector and ArrayList are index based and backed up by an array internally.
  2. 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.
  3. Both Iterator and ListIterator returned by ArrayList and Vector are fail-fast.
  4. ArrayList and Vector also allows null and duplicates.
  5. They both grows and shrinks automatically when overflow and deletion happens.

Differences between Vector and ArrayList


Now let's see some key difference between Vector and ArrayList in Java, this will decide when is the right time to use Vector over ArrayList and vice-versa. Differences are based upon properties like synchronization, thread safety, speed, performance , navigation and Iteration over List etc.

Synchronization and Thread-Safetey


Vector is synchronized while ArrayList is not synchronized. Synchronization and thread safe means at a time only one thread can access the code. In Vector class each method like add(), get(int i) is surrounded with a synchronized block and thus making Vector class thread-safe.

Data Growth


Internally, both the ArrayList and Vector hold onto their contents using an Array. When an element is inserted into an ArrayList or a Vector, the object will need to expand its internal array if it runs out of room. A Vector defaults to doubling the size of its array, while the ArrayList increases its array size by 50 percent.

Performance


ArrayList gives better performance as it is non-synchronized. Vector operations gives poor performance as they are thread-safe. thus in ArrayList two or more threads can access the code at the same time, while Vector is limited to one thread at a time.

fail-fast


First let's understand 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.

Legacy


java.util.Vector class was there in java since the very first version of the java development kit (jdk). java.util.ArrayList was introduced in java version 1.2 , as part of Java Collections framework. In java version 1.2, Vector class has been refactored to implement the List Inteface .

When to use ArrayList and when to use vector?


It actually depends on our need.Vector is slower than ArrayList as its methods are synchronized so if we don't work in multi threaded environment then ArrayList is better choice.

How to make ArrayList synchronized?



Arraylist can be synchronized using:
List list = Collections.synchronizedList(new ArrayList());


Hope we are able to explain you Difference between Vector and ArrayList, if you have any questions or suggestions please write to us using contact us form.(Second Menu from top left).
Difference between ArrayList and Vector in java

Please share us on social media if you like the tutorial.

Source:http://www.tutorialsdesk.com/2014/09/difference-between-arraylist-and-vector.html

Tidak ada komentar:

Posting Komentar