Vector in Java
Vector is a legacy class in Java that provides a dynamic array similar to ArrayList, but with built-in synchronization.
It is part of the java.util package and was introduced in JDK 1.0.
What is Vector?
A Vector is a dynamic, growable array in Java that stores elements and automatically increases its size.
It is similar to ArrayList but thread-safe because all methods are synchronized.
Where Vector Fits in Collection Hierarchy
Iterable
└── Collection
└── List
└── Vector
Vector implements: List, RandomAccess, Cloneable, Serializable
Key Features of Vector (Short Points)
- Grows dynamically when more elements are added
- Thread-safe (all operations synchronized)
- Maintains insertion order
- Allows duplicates and null values
- Legacy class (introduced in Java 1.0)
Why Vector is Called Legacy?
Vector is slower due to synchronization and was replaced by modern classes like ArrayList, but it still exists for backward compatibility.
Creating a Vector
Vector<String> vector = new Vector<>();
vector.add("Java");
vector.add("Python");
vector.add("C++");
System.out.println(vector);
Common Vector Methods

1. Add Methods
add(E e)
Adds an element to the end of the vector.
vector.add("PHP");
add(int index, E element)
Inserts an element at the specified position.
vector.add(1, "HTML");
addElement(E obj)
Legacy method; works same as add().
vector.addElement("Ruby");
2. Get Methods
get(int index)
Returns the element at the given index.
vector.get(1);
firstElement() / lastElement()
Returns the first or last element of the vector.
vector.firstElement();
vector.lastElement();
3. Remove Methods
remove(int index)
Removes the element at a specific index.
vector.remove(0);
removeElement(Object obj)
Legacy method to remove an element.
vector.removeElement("Java");
clear()
Removes all elements.
vector.clear();
4. Search Methods
contains(Object o)
Checks if the vector contains the given element.
vector.contains("Java");
indexOf(Object o)
Returns the first index of the element.
vector.indexOf("Python");
5. Size & Capacity Methods
size()
Returns the number of elements.
vector.size();
capacity()
Returns the internal capacity of the vector.
vector.capacity();
Understanding Vector Capacity
Vector starts with a default capacity of 10 and doubles its size automatically when full.
You can also set custom initial capacity or increment size manually.
Vector<Integer> vector = new Vector<>(5, 2);
Time Complexity of Vector
| Operation | Time Complexity | Notes |
|---|---|---|
| Access by index | O(1) | Direct index lookup |
| Insert at end | O(1) amortized | May resize vector |
| Insert at index | O(n) | Elements shift |
| Remove at index | O(n) | Shifts required |
| Search | O(n) | Linear search |
Vector vs ArrayList
| Feature | Vector | ArrayList |
|---|---|---|
| Thread-safe | Yes | No |
| Speed | Slow | Fast |
| Legacy | Yes | No |
| Growth | Doubles (×2) | 1.5× |
| Use Case | Multithreading | Normal use |
Advantages of Vector
- Thread-safe (synchronized)
- Auto-expanding storage
- Backward compatible with old code
- Works well in multi-threaded apps
Disadvantages of Vector
- Slower than ArrayList
- Higher memory usage due to big growth
- Considered outdated (legacy)
- Synchronization cannot be disabled
Real-World Use Cases
Vector is still used in:
- Multi-threaded applications
- Legacy enterprise systems
- Chat servers
- Real-time multi-threaded programs
