The well-known Java Collections Framework (JCF), introduced in JDK 1.2, contains two of the most essential and widely used collections, ArrayList and Vector. Moreover, these two collections are very similar in concepts and functionality. Therefore they lend themselves well to being compared.
In this short article, I will describe the similarities and differences between these two collections. Clearly, this article is mainly intended for developers learning Java. But, of course, it can also be helpful as a refresher for developers of any experience level.
Vector have several similarities since they are structurally implemented in a very similar way.
1) Both collections represent a linear sequence of elements. Each element is addressable by an index of type
int, ranging from 0 to size-1 (where size results from the
2) Both collections are “dynamically” expandable and resizable (like other collections in the JCF).
3) Both collections are internally based on a single
Object array. This array is managed at “expanded capacity” since, most of the time, the array has a much larger physical capacity than the “logical” number of elements. A new larger array is created only when the physical capacity is insufficient, and then the values of the old array are copied into the new array.
4) Both collections allow the insertion of
5) Since Java 5, both collections, like all the others in the JCF, are “generic” (in the sense of Java generics). Thus, they can (and should always!) be used parameterized like, for example,
6) Both collections implement (either directly or indirectly) all the following interfaces:
java.lang.Iterable<E>(since Java 5)
7) Both collections provide an iterator (
java.util.ListIterator) with fail-fast behavior. The iteration fails by throwing
java.util.ConcurrentModificationException when the iterator determines that the collection has been structurally modified using any method outside the
ListIterator interface. Beware that, said in general, the fail-fast behavior is not 100% guaranteed in the face of unsynchronized concurrent modifications.
Vector also have several differences, mainly because they were introduced in the Java Standard Edition framework at different times and with slightly different goals.
Vector exists since JDK 1.0.
ArrayList exists since JDK 1.2 (when the JCF was also introduced).
Vector has a constructor with two parameters,
ArrayList does not have this constructor.
Vector has several “legacy” methods that
ArrayList does not have:
void addElement(E obj)
void copyInto(Object anArray)
E elementAt(int index)
void insertElementAt(E obj, int index)
boolean removeElement(Object obj)
void removeElementAt(int index)
void setElementAt(E obj, int index)
void setSize(int newSize)
Vector can provide an enumeration (
ArrayList does not offer this feature. Note that the
Enumeration provided by
Vector does not have the fail-fast behavior.
Vector is a thread-safe class because all of its methods are synchronized. Invoking any method acquires the
Vector object’s monitor (the “lock”), ensuring mutual exclusion between threads.
ArrayList is not thread-safe and does not offer any kind of synchronization.
6) The two collections differ in the logic used to calculate the new capacity of the internal array.
Vector, by default, applies a 2x factor, while
ArrayList applies a 1.5x factor (concerning the “current” capacity). Note: This is an implementation detail that can be observed in the specific Java runtime developed by Sun/Oracle.
Vector is the super-class of
Stack (another “legacy” collection since JDK 1.0).
ArrayList is the super-class of some particular and largely unknown classes that are part of the Java management API (package