Comparison between ArrayList and Vector

The well known Java Collections Framework (JCF), introduced in JDK 1.2, contains two basic and widely used collections: ArrayList and Vector. These two collections are very similar in terms of concepts and functionality, therefore they lend themselves well to being compared.

In this short article I am going to describe the similarities and also the differences between these two collections. Clearly, the article is mainly intended for developers who are studying Java collections. But, of course, it can also be useful 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 is the result of 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”, that is, most of the time the array has a much greater physical capacity than the “logical” number of elements. When the capacity is no more sufficient, a new larger array is created and 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.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 (I mean both java.util.Iterator and java.util.ListIterator) that has the fail-fast behavior. The iteration fails by throwing java.util.ConcurrentModificationException as soon as the iterator determines that the collection has been structurally modified using any method outside the Iterator/ListIterator interface. Note that, said in general, the fail-fast behavior is not 100% guaranteed in face of unsynchronized concurrent modifications.

… and the differences

ArrayList and Vector have also several differences mainly because they were introduced in the Java standard framework at different times and with slightly different goals.

1) Vector exists since JDK 1.0, while ArrayList exists since JDK 1.2 (when the JCF was also introduced).

2) Vector has a constructor that receives initialCapacity and capacityIncrement while 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 its methods are synchronized. The invocation of any of its methods acquires the monitor (“lock”) of the Vector object, 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 with respect to the “current” capacity. NOTE: this implementation detail is what can be seen in the Java runtime developed by Sun/Oracle.

7) Vector is the super-class of Stack (another “legacy” collection since JDK 1.0) while ArrayList is the super-class of some particular (and largely unknown and unused) classes that are part of the Java management API (package

Similar Posts