In this article we are going see what is Java Vector, why do we use Vector in java and its commonly used methods with simple examples.
Vector is a legacy collections class in java.util package. Vector class is used to hold a group of elements and it is one of the implementation class for List interface.
ArrayList has the similar features but not synchronized. Vector methods are synchronized, so whenever thread safety required to hold collection of elements Vector is good choice over ArrayList. ArrayList is faster, since it is non-synchronized, while vector operations give slower performance since they are synchronized. If we don’t have any explicit requirements for using either of them, we use ArrayList over vector.
Vector class provides below constructors to create a Vector instance.
There are several ways to iterate Java Vector, in this example demonstrating how we iterate Vector using Enumeration with elemetnts() of Vector class.