Comparison between ArrayList and Vector
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.
The similarities between ArrayList and Vector
ArrayList
and 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 size()
method).
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 null
values.
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, ArrayList<String>
and Vector<String>
.
6) Both collections implement (either directly or indirectly) all the following interfaces:
java.io.Serializable
java.lang.Cloneable
java.lang.Iterable<E>
(since Java 5)java.util.Collection<E>
java.util.List<E>
java.util.RandomAccess
7) Both collections provide an iterator (java.util.Iterator
or 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 Iterator
/ListIterator
interface. Beware that, said in general, the fail-fast behavior is not 100% guaranteed in the face of unsynchronized concurrent modifications.
The differences between ArrayList and Vector
ArrayList
and Vector
also have several differences, mainly because they were introduced in the Java Standard Edition framework at different times and with slightly different goals.
1) Vector
exists since JDK 1.0. ArrayList
exists since JDK 1.2 (when the JCF was also introduced).
2) Vector
has a constructor with two parameters, initialCapacity
, and capacityIncrement
. ArrayList
does not have this constructor.
3) Vector
has several “legacy” methods that ArrayList
does not have:
void addElement(E obj)
int capacity()
void copyInto(Object[] anArray)
E elementAt(int index)
Enumeration<E> elements()
E firstElement()
void insertElementAt(E obj, int index)
E lastElement()
void removeAllElements()
boolean removeElement(Object obj)
void removeElementAt(int index)
void setElementAt(E obj, int index)
void setSize(int newSize)
4) Vector
can provide an enumeration (java.util.Enumeration
), while ArrayList
does not offer this feature. Note that the Enumeration
provided by Vector
does not have the fail-fast behavior.
5) 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.
7) 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 javax.management
).