Java Notes
Learn Java Vector — a synchronized dynamic array, how it differs from ArrayList, why it is considered legacy, and when you might still encounter it.
Vector is a resizable array — exactly like ArrayList — but every method is synchronized. This means only one thread can call a method at a time, making it thread-safe out of the box.
Vector was added in Java 1.0, before the Collections Framework existed in Java 1.2. It is considered a legacy class today. In single-threaded code, it is slower than ArrayList with no benefit. For thread safety in modern code, use Collections.synchronizedList() or CopyOnWriteArrayList.
You will encounter Vector in older codebases and exam questions — which is why it is important to know.
Basic Usage
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
Line by line:
add(),get(),remove()— standard List methods inherited from the Collections Framework.addElement(),elementAt(),firstElement(),lastElement()— legacy methods from the original Vector API before Java 1.2.- Both sets of methods work, but prefer the standard Collection methods in new code.
How Vector Grows
Vector defaults to doubling its internal array when full, whereas ArrayList grows by 50%.
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
capacity() shows internal array size. size() shows actual elements. They differ when the array has extra pre-allocated slots.
Vector vs ArrayList
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
| Feature | Vector | ArrayList |
|---|---|---|
| Thread-safe | Yes (every method synchronized) | No |
| Performance | Slower (lock overhead) | Faster |
| Growth | Doubles by default | Grows 50% |
| Legacy methods | Yes (elementAt, addElement...) | No |
| Introduced | Java 1.0 | Java 1.2 |
Use ArrayList. If you need thread safety, use Collections.synchronizedList(new ArrayList<>()) or CopyOnWriteArrayList from java.util.concurrent.
Common Mistake: Assuming Vector Is Always the Right Choice for Threads
Vector<String> v = new Vector<>();
// This is NOT thread-safe even with Vector:
if (!v.contains("Asha")) { // thread 1 checks
v.add("Asha"); // thread 2 may add between these two lines
}Individual methods are synchronized, but compound operations (check-then-act) are not atomic. You still need external synchronization for compound actions.
Practice Task
Task: Create a Vector<Integer> with values 1 to 10. Use the legacy elementAt() method to print the element at index 4. Use firstElement() and lastElement(). Then convert it to an ArrayList using new ArrayList<>(vector) and print both to confirm they have the same content.
Hint: Vector implements List, so new ArrayList<>(vector) works directly.
Solution:
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
What to observe: vector.equals(arrayList) returns true — both contain the same elements in the same order. Vector and ArrayList both implement List, so their equals() method compares content. After conversion, changes to arrayList do not affect vector.
Continue the lesson path