Wednesday, 26 August 2015

ArrayList vs. Vector


Synchronization and Thread-Safe

ArrayList is not synchronized.

Synchronization and thread safe means at a time only one thread can access the code.

Vector is synchronized.


In comparison ArrayList is fast as it is non-synchronized.

Thus in ArrayList two or more threads can access the code at the same time.

Vector is slow as it is thread safe.

Vector is limited to one thread at a time.

Automatic Increase in Capacity

While when you insert an element into the ArrayList, it increases its Array size by 50%.

By default ArrayList size is 10.

It checks whether it reaches the last element then it will create the new array, copy the new data of last array to new array, then old array is garbage collected by the Java Virtual Machine (JVM). 

A Vector defaults to doubling size of its array.

Set Increment Size

ArrayList does not define the increment size.

There is no setSize() method or any other method in ArrayList which can manually set the increment size.

Vector defines the increment size.

public synchronized void

setSize(int i) {
      //some code


Other than Hashtable, Vector is the only other class which uses both Enumeration and Iterator.

While ArrayList can only use Iterator for traversing an ArrayList.

since the very first version
 java version 1.2 

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...