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 thread safety isn't an issue?

- 15,743
- 6
- 59
- 89

- 3,204
- 6
- 31
- 32
4 Answers
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.

- 44,417
- 8
- 90
- 141

- 4,784
- 7
- 53
- 70
-
2Yep, use ArrayList unless you're targeting J2ME/pre 1.2 J2SE. – Aaron Maenpaa Nov 18 '08 at 23:29
-
I agree; Vector is only for support of old JVMs. Even if you need concurrency, use java.util.concurrent collection or the appropriate Collections.synchronizedXXX wrapper, not Vector (Blechtor!). – erickson Nov 18 '08 at 23:32
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);
}
}
-
2Relying on the synchnizedList wrapper is a common mistake to make in these multi-call situations and leads to hard to find bugs as you think you've done it correctly... – Michael Rutherfurd Nov 18 '08 at 23:49
-
Yeah, there aren't many applications of synchronised lists without further external synchronisation. Swing text's Document has the same problem (some of the wild thread-safety claims have been removed in JDK7). – Tom Hawtin - tackline Nov 19 '08 at 15:11
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.

- 15,743
- 6
- 59
- 89

- 47,767
- 15
- 98
- 117
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.

- 17,844
- 7
- 51
- 64