Is there really that much of a difference between the performance of Vector
and ArrayList
? Is it good practice to use ArrayLists at all times when
Ignoring synchronization, the main difference between Vector
and ArrayList
is that Vector
is a resizable array (similar to a C++ STL Vector) and ArrayList
is a List that happens to be backed by an array.
The difference manifests itself in the setSize() method. There is no equivalent method in ArrayList
. Some ex-C++ Java developers get hung up on this. There are a number of easy ways to work around it so it shouldn't be an issue.
Just don't make the mistake of telling a C++ developer that an ArrayList
is the equivalent to a std::vector
. You'll never hear the end of it.
First and foremost difference between Vector and ArrayList is that Vector is synchronized and ArrayList is not, what it means is that all the method which structurally modifies Vector e.g. add ()
or remove ()
are synchronized which makes it thread-safe and allows it to be used safely in a multi-threaded and concurrent environment. On the other hand ArrayList methods are not synchronized thus not suitable for use in multi-threaded environment.
ArrayList is way faster than Vector. Since Vector is synchronized and thread-safe it pays price of synchronization which makes it little slow. On the other hand ArrayList is not synchronized and fast which makes it obvious choice in a single-threaded access environment.
Whenever Vector crossed the threshold specified it increases itself by value specified in capacityIncrement field while you can increase size of ArrayList by calling ensureCapacity()
method.
Vector can return enumeration of items it hold by calling elements()
method which is not fail-fast as opposed to Iterator and ListIterator
returned by ArrayList
.
Another point worth to remember is Vector is one of those classes which comes with JDK 1.0 and initially not part of Collection framework but in later version it's been re-factored to implement List interface so that it could become part of collection framework
If thread safety isn't an issue you should always use ArrayList
. Vector has the overhead of synchronization and it has been shown that the performance differences between ArrayList
and Vector
are abysmal. You can google for a lot of performance benchmarks.
Here's one Timing & Performance.
Vector originates back from the pre-Collections API days, and have been retrofitted since to be a part of it. From what I've read, the reason it is not deprecated is because the core API depends on it.
ArrayList was written from scratch as a part of the Collections API and as such should be used unless you need to support Java versions down to 1.2.
If you need a thread-safe ArrayList, you can use the static factory method Collections.synchronizedList(new ArrayList<type>);
to generate your list.
If thread safety is not an issue, ArrayList
will be faster as it does not have to synchronize. Although, you should always declare your variable as a List
so that the implementation can be changed later as needed.
I prefer to handle my synchronization explicitly because a lot of operations require multiple calls. For example:
if (!myList.isEmpty()) {
myList.get(0);
}
should be:
synchronized (myList) {
if (!myList.isEmpty()) {
myList.get(0);
}
}