Java Vector
Vector is a legacy collection class in Java that implements the List interface.
Like ArrayList, it provides a dynamically resizable array and allows elements to be accessed using indexes.
Vector<String> names = new Vector<>();
names.add("Rahul");
names.add("Priya");
names.add("Amit");
System.out.println(names);Output
[Rahul, Priya, Amit]Why Does Vector Exist?
Vector was introduced in early versions of Java, before the modern Collections Framework was established.
It was later integrated into the Collections Framework.
A major characteristic of Vector is that many of its methods are synchronized.
Because of this historical design, Vector is generally encountered in older Java code.
Creating a Vector
import java.util.Vector;
Vector<String> names = new Vector<>();We can also specify an initial capacity:
Vector<String> names = new Vector<>(10);Here, the initial capacity is 10.
Adding Elements
names.add("Java");
names.add("Python");
names.add("C++");Vector also provides the older addElement() method:
names.addElement("Java");For new code, the standard List methods are generally clearer.
Accessing Elements
String language = names.get(0);We can also use:
names.elementAt(0);Updating Elements
names.set(1, "JavaScript");Removing Elements
names.remove(0);Vector also provides legacy methods such as:
names.removeElement("Java");Getting Size
System.out.println(names.size());Checking Capacity
Vector provides:
System.out.println(names.capacity());Size represents the number of stored elements.
Capacity represents the amount of storage currently allocated for elements before another capacity expansion may be needed.
For example:
Vector<String> names = new Vector<>(5);
System.out.println(names.size());
System.out.println(names.capacity());Output
0
5Vector Is Dynamically Resizable
Like ArrayList, Vector can grow when more elements are added.
Vector<Integer> numbers = new Vector<>();
for (int i = 1; i <= 20; i++) {
numbers.add(i);
}
System.out.println(numbers);Vector and Synchronization
Many methods of Vector are synchronized.
This means individual method calls provide synchronization around the Vector's operations.
However, this does not automatically make every multi-step operation on a Vector safe without additional synchronization.
For example, a sequence involving multiple separate method calls may require external coordination if it must behave atomically.
Vector vs ArrayList
| Feature | Vector | ArrayList |
|---|---|---|
| Implements List | Yes | Yes |
| Resizable | Yes | Yes |
| Index-based access | Yes | Yes |
| Legacy class | Yes | No |
| Methods synchronized | Many are synchronized | No |
| Typical modern choice | Less common | Common |
| Allows duplicates | Yes | Yes |
| Allows null | Yes | Yes |
Example
import java.util.Vector;
public class HelloWorld {
public static void main(String[] args) {
Vector<String> languages = new Vector<>();
languages.add("Java");
languages.add("Python");
languages.add("JavaScript");
System.out.println("Languages: " + languages);
System.out.println("Size: " + languages.size());
System.out.println("First: " + languages.get(0));
}
}Output
Languages: [Java, Python, JavaScript]
Size: 3
First: JavaWhen Should You Use Vector?
For new application development, ArrayList is generally the more common choice when you simply need a resizable list.
Vector is still important to understand because:
- You may encounter it in existing applications.
- It is part of Java's standard library.
- Some older APIs and codebases use it.
- Its synchronization behavior is relevant when maintaining legacy code.
If thread-safe list behavior is actually required, modern Java code should generally consider the appropriate concurrent collection or explicit synchronization strategy based on the application's needs rather than choosing Vector automatically.
Important Points
- Vector implements List.
It is dynamically resizable. It supports index-based access. Duplicate elements are allowed. null values are allowed. Many of its methods are synchronized. It is considered a legacy collection class. ArrayList is generally preferred for ordinary modern list usage.
Key Takeaway
Vector is a dynamically resizable list that predates the modern Collections Framework and provides synchronized methods. It remains useful to understand when working with legacy Java applications, but it is not usually the first choice for new list-based code.
See you in next page....🙂