Why Vector class is obsolete in Java?

In this post, we will explore the major reasons behind Vector class being obsolete in Java.


The Vector class is considered as a legacy class in Java. It was first introduced with JDK 1.0, and it was later retrofitted to implement the List interface. Even though it is now a member of the Java Collections Framework, it is rarely used in new projects and supported only for backwards compatibility.

In this post, we will highlight the flaws in Vector class implementation which lead to Vector class being obsolete.


1. Bad Design

The major drawback of Vector class is that it is synchronized but not completely thread-safe.


This is because Vector synchronizes on each individual operation and does not synchronize the whole Vector instance itself. This is not desired in real-world applications where whole set of operations needs to be synchronized and not individual operations. If one thread is iterating over the Vector and another thread modifies the Vector instance structurally, the iterator will throw a ConcurrentModificationException.

In other words, two threads can operate on a Vector instance at the same time provided they’re performing different operations.


2. Performance

The Vector class combines two features – resizable-array and synchronization. Therefore it has the overhead of locking the operations whether synchronization is required or not, which impacts the performance.

We have seen that the Vector synchronizes on each individual operation and does not synchronize the whole Vector instance itself. This also causes performance issues as we will be acquiring the lock again and again for each operation. Acquiring the lock once for whole Vector instance is much more efficient.


3. Supports Enumerations

Vector supports some legacy methods like elements() which returns an enumeration of the components of the vector. Programmers prefer using Iterator or ListIterator in preference to Enumeration because:

  1. The remove() method is not defined for Enumeration. That means the list cannot be structurally modified done during iteration.
  2. Unlike ListIterator, Enumeration doesn’t offer bi-directional access.
  3. Iterator has shorter method names – hasMore() and next(), as opposed to hasMoreElements() and nextElement() of Enumeration interface.


Alternatives to the Vector class –

As per Java documentation of the Vector class, if a thread-safe implementation is not needed, it is recommended to use ArrayList in place of Vector.

If a thread-safe implementation of List interface is required, we can either use CopyOnWriteArrayList class, which is a thread-safe variant of ArrayList or synchronize ArrayList externally using the synchronizedList() method of the Collections class.

References: Vector and Enumeration Javadoc

Thanks for reading.

Please use ideone or C++ Shell or any other online compiler link to post code in comments.
Like us? Please spread the word and help us grow. Happy coding 🙂

Leave a Reply

Notify of